1

How can I pass extras to activity which is previous in backstack? When user press BACK button I would like to do something like:

intent.putExtra("playlist", playlist);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

I guess it has to be in onStop() method but how can I identify an activity that will be resumed? I found somthing like getParentActivityIntent() but I cannot understand documentation clearly and I do not know if it is what I need.

-----EDIT------

I will try to explain it other way.

in ActivityA I start a new ActivityB and pass some Extra:

Intent intent = new Intent(this, ActivityB.class);
int var = 3;
intent.putExtra("var", var);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

in ActivityB I can do:

if (getIntent().hasExtra("var")) {
  var = getIntent().getIntExtra("var");
  var = var + 2;        
}

Now after pressing BACK button the user go back to ActivityA. Where var = 3. But I would like to pass the new value of var from ActivityB. I would like to have in ActivityA var = 5. How can I do this? I cannot find in documentation a reference to "previous" activity (in this case to ActivityA from ActivityB).

Malvinka
  • 1,185
  • 1
  • 15
  • 36
  • The code you've posted isn't clear. Where is it? In an `Activity` or in a `Fragment`? – Squonk Oct 23 '14 at 18:18
  • It's in Activity. I use it normally when I start new activity. ex. after some action (choosing list item, clicking software button etc). Now I would like to do the same for phisical back button. – Malvinka Oct 23 '14 at 18:26

1 Answers1

3

I think that what you trying to do isn't possible, but you have at least two options.

Not use startActivity, but use startActivityForResult in ActivityA and override onActivityResult. In Activity B you will have to implement in onBackPressed something like this:

Intent returnIntent = new Intent();
returnIntent.putExtra("result",result);
setResult(RESULT_OK,returnIntent);
finish();

More info: How to manage `startActivityForResult` on Android?

Second option is to create shared variable during whole application (e.g. by extend android.app.Application class). Set this varible in ActivityB and read in ActivityA.

Community
  • 1
  • 1
vzoha
  • 151
  • 5
  • It seems that it will be working (I used first solution). However I faced another problem so I have to solve it to be sure everything's ok with this one. Thanks anyway for your time! – Malvinka Oct 23 '14 at 19:22
  • It's OK, if it will help, please accept answer. Thanx. – vzoha Oct 23 '14 at 20:33