简体   繁体   中英

Saving state of fragment using DrawerLayout

I used this tutorial to make the SliderView.

In the first fragment I have a media player with pause button, play button, and stop button. When I press the play button, it enables the others, and disable itself. After that, I go to the other fragment using the Slider Menu. And when I return to the first fragment where my media player is, the buttons are in the initial state. It calls the onCreateView again.

I need to know how to save the state of that fragment to show it later.

Take a look at the Fragment docs over at Android's developer site. In their example they show clearly how to save state here . For ease of use I'll copy here :).

Basically, you have to check for saved state in onActivityCreated and save any desired info in onSaveInstanceState as follows:

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    .....

    if (savedInstanceState != null) {
        // Restore last state for your player information.
        mIsPlaying = savedInstanceState.getBoolean("isPlaying", false);
    }
    ......
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putBoolean("isPlaying", mIsPlaying);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM