简体   繁体   English

将结果传递给另一个活动

[英]Passing result to another Activity

I'm writing an application for Android and I have difficulties with change the Buttons background. 我正在为Android编写应用程序,但在更改Buttons背景方面遇到困难。 I have four Activities, Activity1 is a TableLayout where I have three Buttons. 我有四个Activity,Activity1是一个TableLayout,其中有三个Button。 Every Button opens another Activity. 每个按钮都会打开另一个活动。 I'd like to change the Button in Activity1 from Activity2. 我想从Activity2更改Activity1中的按钮。 So I tried to do it with passing a Result. 所以我尝试通过传递结果来实现。 In Activity2 I set a Result like this: 在Activity2中,我将结果设置如下:

@Override
public void onClick(View v) {
        setResult(Activity1.RESULT_OK);
        finish();
    }
}

In Activity1 I have this code: 在Activity1中,我有以下代码:

          protected void onActivityResult(int requestCode, int resultCode, Intent data){
          if (resultCode == RESULT_OK)
          button1.setBackgroundResource(R.drawable.image);    
          }

So when the user click the Button in the Activity2 then in the Activity1 the Button change the background. 因此,当用户单击Activity2中的Button时,然后在Activity1中,Button会更改背景。 My problem is that I can do that only once and I have to do that two more times in the other Activities. 我的问题是,我只能做一次,而在其他活动中我必须再做两次。 I tried to do RESULT_OK2 but it shows me error. 我尝试做RESULT_OK2,但显示错误。 So how can I do it more times? 那我怎么能再做几次呢? I tried to change the background another way. 我试图用另一种方式改变背景。 In Activity2 I used the button1 which is in Activity1 but I then I got NullPointerExeption. 在Activity2中,我使用了Activity1中的button1,但是随后我得到了NullPointerExeption。 If anyone has an idea how to that please response! 如果有人对此有任何想法,请回复!

You can compare the requestCode too (this is the request code you passed on startActivityForResult) 您也可以比较requestCode(这是您在startActivityForResult上传递的请求代码)

This will avoid the RESULT_OK being interpreted to every activity result. 这样可以避免将RESULT_OK解释为每个活动结果。

I tried to do RESULT_OK2 我试图做RESULT_OK2

There is no Activity constant for RESULT_OK2 so that's why you get the error 没有RESULT_OK2 Activity常量,所以这就是为什么会出现错误

You can pass back Intent Extras . 您可以传回Intent Extras Something like 就像是

@Override
public void onClick(View v) {
    // add the intent info
    Intent i = new Intent(); // make sure to use empty constructor
    i.putExtra("image", someVar); // might want activity const for key and someVar can be String, int, or whatever you want to use 
    setResult(Activity1.RESULT_OK, i);
    finish();
}
}

then check for that var in your onActivityResult() in the data param and set the image appropriately. 然后在data参数的onActivityResult()中检查该onActivityResult() ,并适当设置图像。

Going back to previous activity 返回上一个活动

when you start the activity for result in android you have to pass request code and based on that request code you can make the conditions in the onActivityResultMethod.

**Button 1**
Intent i = new Intent(this, yourclass1);
startActivityForResult(i, 1);

**in yourclass1**
setResult(RESULT_OK);
finish();

**Button 2**
Intent i = new Intent(this, yourclass2);
startActivityForResult(i, 2);

**in yourclass2**
setResult(RESULT_OK);
finish();

**Button 3**
Intent i = new Intent(this, yourclass3);
startActivityForResult(i, 3);

**in yourclass3**
setResult(RESULT_OK);
finish();

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == 1 && resultCode == RESULT_OK) {
       // change background of button 1
    }else if (requestCode == 2 && resultCode == RESULT_OK) {
       // change background of button 2
    }else if (requestCode == 3 && resultCode == RESULT_OK) {
       // change background of button 3
    }
}

You can store a value in SharedPreference and based on the value in SharedPreference, you can change the background color of button1. 您可以将值存储在SharedPreference中,并基于SharedPreference中的值,可以更改button1的背景颜色。

In Activity 2, 在活动2中,

@Override
public void onClick(View v) {
    SharedPreferences.Editor editor = getSharedPreferences("RESULTS", MODE_PRIVATE).edit();
    editor.putInt("BUTTON1_bg", 1);
    editor.commit();
    finish();
}

In Activity 1 活动1

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      SharedPreferences prefs = getSharedPreferences("RESULTS", MODE_PRIVATE); 
      int bg = prefs.getInt("BUTTON1_bg");
      // based on value of  bg, you can decide what background to use for button1
    }

You need to start all the three activity with different request code. 您需要使用不同的请求代码启动所有三个活动。 Or get the intent data to distinguish. 还是得到意图数据来区分。 You can do something like From Activity1 start all activity with a requestCode as 您可以执行以下操作:从Activity1开始,所有活动都带有requestCode

startActivityForResult( <intent>,<an requestCode>);

Then you can filter the same requestCode in onActivityResult() as 然后,您可以在onActivityResult()过滤相同的requestCode作为

if(requestCode==11){
   button1.setBackgroundResource(R.drawable.image);  
}else if(requestCode==22){
   button2.setBackgroundResource(R.drawable.image);  
}else if(requestCode==33){
   button3.setBackgroundResource(R.drawable.image);  
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM