1

I have an activity (MainActivity) which has a navigation drawer and can show 2 fragments (Fragments A and B), one at a time. (This activity is the default activity with navigation drawer created by android studio)

When I choose fragment B on the drawer the action bar menu is updated to show a button specific for fragment B (Button P).

Button P open an independent activity (IndependentActivity) with an explicit intent, on this activity I perform a database operation and after it I finish this activity to go back to MainActivity.

The problem is: When IndependentActivity is finished, MainActivity is shown but it shows fragment A instead of fragment B which was the one that called the intent to go to IndependentActivity.

How do I fix this by showing the fragment that initiated the action to go to another activity? Is there any way to save the fragment that was appearing?

biamacedo
  • 11
  • 2

2 Answers2

0

Basically what you have to do is:

  • Save the state of the MainActivity when you enter IndependentActivity
  • Reload the state of MainActivity when you exit IndependentActivity

A simple implementation of this could be:

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    // Save which fragment that is in the view when entering another activity
    savedInstanceState.putString("fragment", "fragmentB");
    super.onSaveInstanceState(savedInstanceState);
}

Fragment B can be replaced with a string that tells the application which is the current fragment.

Once you come back to the activity you want to:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if( savedInstanceState != null ) {
         // Get which fragment that was active when you left the activity
         savedInstanceState.getString("fragment");

         // Programatically select the fragment here
    }
}

You can read more about saving instance state here:

http://developer.android.com/training/basics/activity-lifecycle/recreating.html

I will leave it to you to programmatically select the fragment. I hope it helps!

Prince John
  • 592
  • 1
  • 3
  • 16
  • Thanks for the quick response! I tried to save the number of the fragment to call it again on a switch statement but debugging I found that on the onSaveInstanceState method the bundle is saved correctly but when the onCreate method is called the bundle is null. Do you have any ideas why this is happening? – biamacedo Sep 07 '15 at 20:32
0

I found out that I was having the same problem as this: https://stackoverflow.com/a/29464116/2325672

The back arrow to return did not work and reset the activity's fragments and the emulator back triangle worked perfectly.

Adding this code to IndependentActivity worked for me:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId()== android.R.id.home) {
        Intent intent = NavUtils.getParentActivityIntent(this);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        NavUtils.navigateUpTo(this, intent);
        return true;
    }
    return super.onOptionsItemSelected(item);
}
Community
  • 1
  • 1
biamacedo
  • 11
  • 2