Search code examples
androidandroid-fragmentsandroid-activityandroid-fragmentactivityonresume

How to fire onResume() on top most fragment only, on back press


I have 2 Activity(s). Inside 1st Activity there is initially one Fragment

MainActivity.java

public class MainActivity extends Activity {
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);
        setContentView(R.layout.myactivity);
        if(savedInstanceState == null) {
            getFragmentManager().beginTransaction().replace(R.id.fragmentContainer, MainFragment.newInstance().commit();
        }
    }
}

then clicking on a button replaces it with another Fragment.

@Override
public void onClick(View arg0) {
    DetailFragment detail = (DetailFragment)getFragmentManager().findFragmentById(R.id.detail);
    getFragmentManager().beginTransaction().replace(R.id.detail, detail, "detail").commit();
}

On 2nd Fragment there is another button, clicking on it opens a New Activity.

Intent popUp = new Intent(MainActivity.this, PopUp.class);                      
popUp.putExtra("CarID", carID);
startActivity(popUp);

From PopUp Activity, pressing device back will go back to MainActivity. Now the challenge is for the Application's business logic I need to update the ActionBar's title of previous MainActivity when user goes back.

For this I'm listening for onResume() on both MainFragment and DetailFragment. Also when user goes back from DetailFragment to MainFragment I update the ActionBar title with different text.

So I need to know when exactly user goes back from:

1) PopUp Activity > Detail Fragment

2) Detail Fragment > Main Fragment

Currently onResume() is fired on both MainFragment and DetailFragment when PopUpActivity is closed. On MainFragment I can't exactly find out whether onResume() is called for 1st or 2nd case.

What is the best practice to fire onResume() on DetailFragment only when user goes back from PopUpActivity > DetailFragment. In other words, how do I detect from DetailFragment that PopUpActivity is closed without firing onResume() on MainFragment.


Solution

  • I wouldn't mess with onResume() for something like this.

    I would suggest doing the following:

    • Create a Stack<String> for titles.
    • Implement FragmentManager.OnBackStackChangedListener in your MainActivity.
    • In your onBackStackChanged() implementation, check if the back stack has been pushed or popped using FragmentManager.getBackStackEntryCount().
      • If the back stack has been pushed, push the newly-displayed fragment's title to your title stack.
      • If the back stack has been popped, pop a title and set the title bar with the title at the new top of stack.
    • If you set a title from a fragment that isn't added to the back stack, pop a title from the title stack and push the newly-displayed fragment's title, i.e. replace the title at the top of the stack.
    • Invoke your PopupActivity with startActivityForResult() instead of startActivity().
    • Override onActivityResult() in your MainActivity so that when PopupActivity returns, you set the title bar with the title at the top of stack.
    • Don't forget to persist your title stack in onSaveInstanceState() in your MainActivity and restore it in onCreate().

    That might seem like a lot of work just for maintaining titles, but you will drive yourself crazy trying to do this with onResume.