ANDROID - 2个不同的意图打开同一活动 [英] ANDROID - 2 different intents opens the same activity

查看:248
本文介绍了ANDROID - 2个不同的意图打开同一活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个应用程序,并在这一点上我有携带图像两种不同的意图。我正在试图通过在同一个活动的图像imageViews。

谁能帮助?谢谢!

我的code是:

 的ImageButton btn_insert =(的ImageButton)findViewById(R.id.btn_insert);
    btn_insert.setOnClickListener(新View.OnClickListener(){
        @覆盖
        公共无效的onClick(视图v){            意图I =新意图(getApplicationContext(),ViewClothes.class);
            i.putExtra(ITEM_IMAGE1,此搜索);
            startActivity(ⅰ);            意图I2 =新意图(getApplicationContext(),ViewClothes.class);
            i2.putExtra(ITEM_IMAGE2,IMAGE2);
            startActivity(I 2);
        }
    });

而在第二个活动:

  @覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);    的setContentView(R.layout.view_clothes);    意向I = getIntent();
    ImageView的此搜索=(ImageView的)findViewById(R.id.im1);
    image1.setImageBitmap(BitmapFactory.de codeFILE(i.getStringExtra(此搜索)));    意向I2 = getIntent();
    ImageView的IMAGE2 =(ImageView的)findViewById(R.id.im2);
    image2.setImageBitmap(BitmapFactory.de codeFILE(i2.getStringExtra(图像2)));}


解决方案

您dubbled的意图,你推出一个活动两次。

一个单一的意图可以有多个对象和这些信息传递给一个活动。一个目的是用于启动下一个活动,但您可以轻松添加与它的几个对象如下:

  //第一项活动
意图I =新意图(getApplicationContext(),ViewClothes.class);
i.putExtra(ITEM_IMAGE1,此搜索);
i.putExtra(ITEM_IMAGE2,IMAGE2);
startActivity(ⅰ);

和接收到的所有图像,例如:

  //下一个活动
ImageView的此搜索=(ImageView的)findViewById(R.id.im1);
ImageView的IMAGE2 =(ImageView的)findViewById(R.id.im2);意向I = getIntent();
image1.setImageBitmap(BitmapFactory.de codeFILE(i.getStringExtra(ITEM_IMAGE1)));
image2.setImageBitmap(BitmapFactory.de codeFILE(i.getStringExtra(ITEM_IMAGE2)));


另一种解决方案可能是使用一个字符串数组为您的多个图像。在第一个活动,你可以填充数组:

  //填充数组
的String [] =影像新的String [] {此搜索,图像2};
//传递数组
意图I =新意图(getApplicationContext(),ViewClothes.class);
i.putExtra(ARRAY_IMAGES,图像);

和通入意向检索它是这样的:

  //检索
的String [] = images_passed getIntent()getStringArrayExtra(ARRAY_IMAGES)。
//显示图像
image1.setImageBitmap(BitmapFactory.de codeFILE(images_passed [0]));
image2.setImageBitmap(BitmapFactory.de codeFILE(images_passed [1]));

I'm making an app and at this point i have two different intents carrying images. I' am trying to pass those images in the same activity in imageViews.

Can anyone help? Thanks!!

My code is:

ImageButton btn_insert = (ImageButton)findViewById(R.id.btn_insert);
    btn_insert.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent i  = new Intent(getApplicationContext(), ViewClothes.class);
            i.putExtra(ITEM_IMAGE1, image1);
            startActivity(i);

            Intent i2 = new Intent(getApplicationContext() , ViewClothes.class);
            i2.putExtra(ITEM_IMAGE2 , image2);
            startActivity(i2);


        }
    });

And in the second activity:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.view_clothes);

    Intent i = getIntent();
    ImageView image1 = (ImageView)findViewById(R.id.im1);
    image1.setImageBitmap(BitmapFactory.decodeFile(i.getStringExtra("image1")));

    Intent i2 = getIntent();
    ImageView image2 = (ImageView)findViewById(R.id.im2);
    image2.setImageBitmap(BitmapFactory.decodeFile(i2.getStringExtra("image2")));



}

解决方案

You dubbled the Intent and you launched the next Activity twice.

A single Intent can have multiple objects and pass those to an Activity. One Intent is used to launch the next Activity but you can easily add several objects with it as follows:

// first activity
Intent i = new Intent(getApplicationContext(), ViewClothes.class);
i.putExtra(ITEM_IMAGE1, image1);
i.putExtra(ITEM_IMAGE2 , image2);
startActivity(i);

And received all images like:

// next activity
ImageView image1 = (ImageView)findViewById(R.id.im1);
ImageView image2 = (ImageView)findViewById(R.id.im2);

Intent i = getIntent();
image1.setImageBitmap(BitmapFactory.decodeFile(i.getStringExtra(ITEM_IMAGE1)));
image2.setImageBitmap(BitmapFactory.decodeFile(i.getStringExtra(ITEM_IMAGE2)));


Another solution might be to use a StringArray for your several images. In the first Activity, you could populate the array:

// populate the array
String[] images = new String[] { image1, image2 };
// pass the array
Intent i = new Intent(getApplicationContext(), ViewClothes.class);
i.putExtra(ARRAY_IMAGES, images);

And pass into the Intent to retrieve it like this:

// retrieve it
String[] images_passed = getIntent().getStringArrayExtra(ARRAY_IMAGES);
// show the images
image1.setImageBitmap(BitmapFactory.decodeFile(images_passed[0]));
image2.setImageBitmap(BitmapFactory.decodeFile(images_passed[1]));

这篇关于ANDROID - 2个不同的意图打开同一活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆