简体   繁体   中英

Passing result to another Activity

I'm writing an application for Android and I have difficulties with change the Buttons background. I have four Activities, Activity1 is a TableLayout where I have three Buttons. Every Button opens another Activity. I'd like to change the Button in Activity1 from Activity2. So I tried to do it with passing a Result. In Activity2 I set a Result like this:

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

In Activity1 I have this code:

          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. 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. 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. 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)

This will avoid the RESULT_OK being interpreted to every activity result.

I tried to do RESULT_OK2

There is no Activity constant for RESULT_OK2 so that's why you get the error

You can pass back 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.

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.

In Activity 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

    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

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

Then you can filter the same requestCode in onActivityResult() as

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);  
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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