0

I have a music player app I created, while Im playing music and exit the app by pressing home or back button and then launch the app again, it doesn't return to the current state where I exit the app and also the song index goes back to its default value which is 1 but the music is still playing. and when I play music again, the current song I played is still playing. therefore, there are two songs are playing. How can I save the current state of my application?

3 Answers3

0

You can save the states in onPause() and can retrieve them back in onresume()

Or you can use onwindowFocusChanged() :

public void onWindowFocusChanged(boolean hasFocus) {

    super.onWindowFocusChanged(hasFocus);
    if(hasFocus) 
        //retrive state
    else
        //save state
}
Sushil
  • 8,250
  • 3
  • 39
  • 71
0

Short answer: You really oughta need to read this activity page which tells you about the life cycle of an activity..

Long answer : what's going on is all your variables are reloading (probaly through onResume or onStart) so you lose state. You can easily save the state of some variable using onSaveInstanceState(Bundle b) link

Then reload your variables using onLoadInstanceState.. Easy as that..

More details can be found in this answer; https://stackoverflow.com/a/151940/137404

Community
  • 1
  • 1
Tolga E
  • 12,188
  • 15
  • 49
  • 61
0

es you can save your current state . just add the following code in your menifest file .

        <activity android:name=".Song_list"
        android:configChanges="keyboardHidden|orientation|screenSize"
      //the line below will solve your problem just put it in every activity that you created
        android:launchMode="singleTop">
        </activity>