1

I implemented an activity with a navigation drawer and a content fragment. The content fragment is replaced many times. But at some point I must provide a jumpback to a specific fragment with a specific state. I managed to save any required variables. And it works often but not always to jump back.

I make a button in the actionbar visible if the specific fragment calls their onPause() method. But, since the life cycle of a fragment is tied to the activity's and the activity is not changing, sometimes the button does not appear because onPause() is not called.

So: Which method is always triggered if a fragment is not anymore in foreground? I tried: onPause, onStop, onHiddenChanged()....

Update: Here is my code to save the game state, which is placed in the fragment. Until now it is placed in the onStop() method. The gamelogic variable is an object with many many values and collections. I save this to the shared preferences.

@Override
    public void onStop() {
        super.onStop();

        if(!isGameDone){    
            SharedPfref sharedPfref = new SharedPfref();
            sharedPfref.saveGameLogicInSharedPref(this.gameLogic);
        } else {
            SharedPfref sharedPfref = new SharedPfref();
            sharedPfref.deleteGameLogicFromSharedPref();
        }

    }

In my main activity I decide to display an icon at the action bar or not. By a press on this button I restart the fragment with the saved game state holder.

@Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        SharedPfref sharedPfref = new SharedPfref();
        if(sharedPfref.isGameLogicSavedInSharedPref()){
            menu.findItem(R.id.action_resume_game).setVisible(true);
        } else {
            menu.findItem(R.id.action_resume_game).setVisible(false);
        }


        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (mDrawerToggle.onOptionsItemSelected(item)) {

            return true;
        }

        switch(item.getItemId()) {
            case R.id.action_resume_game:

                SharedPfref sharedPfref = new SharedPfref();
                GameLogic gameLogic = sharedPfref.loadGameLogicFromSharedPref();

                FragmentManager fragmentManager = getSupportFragmentManager();
                fragmentManager.beginTransaction()
                        .replace(R.id.content_frame, GameFragment.newInstance(gameLogic))
                        .commit();


                return true;
            default:
                return super.onOptionsItemSelected(item);
        }

    }
Bolic
  • 705
  • 2
  • 12
  • 30
  • Can you post what you do when you change the fragment?, because de documentation in the fragment lifecycle say onPause() is always called. – Akariuz May 26 '15 at 18:32
  • 1
    onPause or onDestroy may not be triggered in all cases. If the Fragment is displayed on the initial main screen, then onDestroy could only happen when you exit the app. If you want to "jumpback", use FragmentTransaction, and pass the saved states from the Main Activity. I can only provide sample code if you post code. – The Original Android May 26 '15 at 20:45
  • And The Original Android is right. The Fragment is on the initial main screen. I am using the navigation drawer and so the fragments hold all the content and there is only one fragment visible at time. If I navigate anywhere I replace the fragment, but often the fragment's methods like onPause and onStop are not called. – Bolic May 28 '15 at 08:42

1 Answers1

2

I saved the following image to my computer so I can refer to it quickly. I think it's pretty self explanatory. I'd try onDestroy if onPause and onStop haven't been cutting it.fragment lifecyle

roarster
  • 4,058
  • 2
  • 25
  • 38
  • But onDestroy() is not always called, so it would have the same behavior he has now. – Akariuz May 26 '15 at 18:39
  • Yes, Akaruiz is right. The methods like onPause, onStop etc. are all tied to the equivalent ones of the activity and they are only called if the activity state move to Pause or Stop. – Bolic May 28 '15 at 08:37
  • @Bolic I wouldn't say that's what he means, and I would argue they're not linked like that. You can have multiple `Fragments` in an `Activity` at different points in time. Say you start with `FragmentA`, then you replace it with `FragmentB`. `FragmentA` will call its `onPause` when you remove it, but the `Activity` hasn't called its own `onPause` . – roarster May 28 '15 at 08:41
  • Hm kay, I do not tested how this react with multiple fragments in one activity. But I if I use only one fragment this always occurs. I am using the fragment as content view and replace them when the user navigates (Navigation Drawer) – Bolic May 28 '15 at 08:45