質問

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);
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top