1

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!

Endi Tóth
  • 221
  • 3
  • 15
  • What do you mean by **i can do that only once and I have to do that two more times** – Abhinav Arora Jul 28 '15 at 19:03
  • I have to change the other Buttons in Acivity1. From Activity2 I'd like to change Button1 background in Activity1. From Activity3 I'd like to change Button2 background in Activity1. So when I send a RESULT_OK from Activity2 to Activity1 I can't send RESULT_OK again from Activity3. – Endi Tóth Jul 28 '15 at 19:09

5 Answers5

1

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.

Marcos Vasconcelos
  • 18,136
  • 30
  • 106
  • 167
0

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

Community
  • 1
  • 1
codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • Thank you for your answer! It's almost good but I have a little problem. When I use finish(); after setResult my Activity go back to Activity1 in the moment I click on the button. But when I don't use finish(); the background dosen't change. I put return after finish(); but nothing happened. Could you help me please? – Endi Tóth Jul 28 '15 at 21:15
  • I guess I'm confused. I thought you wanted to go back to Activity1? – codeMagic Jul 28 '15 at 22:18
  • Sorry I didn't explain my problem clearly. I want to go back but only with backbutton. I have other things to do in Activity2 and I don't want to go back automatically only when the user click the backbutton. – Endi Tóth Jul 28 '15 at 22:25
  • Then save whatever you want in i.putExtra("image", someVar); to `someVar` in your `onClick()` and put the above code to send back the result in `@Override onBackPressed()` http://stackoverflow.com/questions/18337536/android-overriding-onbackpressed/18337567#18337567 – codeMagic Jul 28 '15 at 22:28
0
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
    }
}
Deepak Goyal
  • 4,747
  • 2
  • 21
  • 46
  • Thank you for your answer! It's almost good but I have a little problem. When I use finish(); after setResult my Activity go back to Activity1 in the moment I click on the button. But when I don't use finish(); the background dosen't change. Could you please help with this? – Endi Tóth Jul 28 '15 at 20:24
  • when you call finish() the current activity in the foreground is finished and it passed the result to calling activity after finish(). If you have any issue or query, please let me know. – Deepak Goyal Jul 28 '15 at 23:11
0

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
    }
Bajji
  • 2,243
  • 2
  • 22
  • 35
0

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);  
}
Sanjeet A
  • 5,171
  • 3
  • 23
  • 40