Question

In the main activity ActivityA I replace FragmentA by fragment FragmentB. From FragmentB the user can start a new activity ActivityB. By hitting the back button in ActivityB, ActivityA is displayed showing FragmentA. I was expecting to see FragmentB with its last state. Do I have to save the state of the previous activities separately to provide this behaviour?

ActivityA(FragmentA) -> ActivityA(FragmentB) -> ActivityB 
BACK
ActivityA(FragmentB)

In the main activity I set the current fragment using:

FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
               .replace(R.id.a_main_frame_content, new FragmentB())
               .addToBackStack(null)
               .commit();

From the fragment I start a new activity using:

Intent intent = new Intent(getActivity(), ActivityB.class);
getActivity().startActivity(intent);

ActivityA is set as parent activity for ActivityB to provide proper navigation.

[UPDTATE] It looks like the problem lies in the different behaviour of navigating back and navigating up. If I navigate back, the activity is displayed in its last state while navigating up forces the activity to recreate.

Was it helpful?

Solution

Lets try this:

In the intent of the parentActivity(if you can set it before you create parentActivity its best, otherwise you may have to use setIntent):

    currentActivityIntent.putExtra("random-unique-key-for-each-activity",
random-unique-key-for-each-activity);

And before you create a child activity, u put following in a map:

myKeyIntentMap.put(random-unique-key-for-each-activity, currentActivityIntent);

In the method triggered on "Up" event :

{
String parentKey = currentActivity.parentActivity.getIntent.getStringExtra("random-unique-key-for-each-activity");
Intent intentToLaunch = (Intent)myKeyIntentMap.get(parentKey);
intentToLaunch.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP );
startActivity(intentToLaunch);
}

This way, using the intent, even if your History Stack is A-someAct1-someAct2-B, and u launch intent resolving to A, it will be "brought to front" killing someActs.

P.S. I havent done any null checks and havent kept in mind the exact method names, just given you an approach.

OTHER TIPS

Behaviour of "up" is sometimes misleading indeed. When I faced similar problem I preferred to save my time and not deal with saving states. You can quickly solve it by catching your navigation "up" event in your Activity:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == android.R.id.home) {
        onBackPressed();
    }
    return true;
}

This is happening because when you are moving to ActivityB activityA is going to puse and destroy state respectively, thus when you back to activity ActivityA ActivityA is starting again thus you are getting fragmentA as view. you need to save the state using sharedPfer. Use some flag to save the state in your onCreateView() check the state and set correct fragment for the view. Hope you have got my point.

public static final String PREFS_NAME = "mypref";
boolean isVisited;
//check sharedpref
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
 isVisited= settings.getBoolean("isVisited", false);

if(!isVisited){
// set fragmentA
}else{ 
// set fragmentB
}


// inside fragment transaction block

Editor edit = settings.editor();
isVisited.setBoolean(true);
edit.commit();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top