3

I have an Activity A which I launch with some extras(i.e. put some data to the Intent that starts the Activity). From this Activity I launch another Activity B. The problem is that when I get back from B to A, by calling finish(), A Activity's getIntent() method returns an Intent that contains no extras. So, my questions is: Is this normal behavior? And is there some way that I can signal Activity A that I want it to keep the Intent it was started with. Oh, and please note that I've overridden the onSaveInstanceState() in Activity A.

EDIT: I'm no longer experiencing this behavior on my device. And I haven't changed anything in the code. I'm using a 3 year old device for testing. I wonder if this might have been caused by a glitch on the device?

Milan
  • 900
  • 2
  • 14
  • 25
  • 1
    See this answer http://stackoverflow.com/questions/20558689/back-to-previous-activity-with-intent/20558774?s=18|0.1089#20558774 – codeMagic Apr 30 '15 at 18:01

3 Answers3

4

I got it figured out. This issue only happens when I click on ActionBar's back arrow and not when I press the device's Back button. Because when Back button is pressed B Activity's finish() method is called and the app normally returns to the already created instance of Activity A, when ActionBar's back arrow is clicked by default the Activity B doesn't call the finish() method, instead it creates a new instance of the Activity A due to the Android lateral navigation functionality. So the solution was to Override the ActionBar's back arrow click functionality like this(method added to Activity B):

@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
    int itemId = menuItem.getItemId();
    if (itemId == android.R.id.home) {
        onBackPressed();
        return true;
    }
    return super.onOptionsItemSelected(menuItem);
}
Milan
  • 900
  • 2
  • 14
  • 25
1

Is this normal behavior?

yes it is. Start B with startActivityForResult , and in A, override onActivityResult. Before finishing B call setResult(int, intent), filling up the intent with the data you want to return to A. onActivityResult get the same intent, as third parameter.

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
1

Yes The Behavior is Normal. If You want to start an Activity X from an Activity Y and get some data from X and return to Y then you have to use startActivityForResult(intent,request_code) Then you need to override theonActivityResult()` method In the Activity X after creating the intent and putting the data in the intent you need to do the following

setResult(RESUKT_OK,i);

`

Pallav
  • 165
  • 1
  • 2
  • 14