0

basically my app has 2 activities.Say "A" and "B" . A launches B.

Activity B plays music and also has a notification.

Case 1:when the view is still on activity B` i press home button and then i click on the the notification, activity B is opened with its view intact and with its music playing (because in the manifest i am using android:launchMode= "singleTop" and hence another instance of the activity is not created) this part is as desired...... but

Case 2:when the view is on activity B and i press back button ,activity A appears and then i click on the the notification, activity B is opened with the view lost and music also stops(not desired)......i am making a guess that it happens because when i press the back button the activity is destroyed ,so i have to progamatically restore its view right??so to restore its view i override two methods .....

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
  // Save UI state changes to the savedInstanceState.
  // This bundle will be passed to onCreate if the process is
  // killed and restarted.
  savedInstanceState.putBoolean("boolMusicPlaying", boolMusicPlaying);
  savedInstanceState.putInt("swapnumber", swapnumber);
  savedInstanceState.putString("seekbarprogress", progress2);
  savedInstanceState.putInt("position.seekbar",seekbar.getProgress());
  savedInstanceState.putString("seekmaxString", max2);
  savedInstanceState.putInt("seekmaxInt",seekMax);
  savedInstanceState.putParcelableArrayList("songfetails",songdetails);
  super.onSaveInstanceState(savedInstanceState);


}
//make a note ....even if i don't override onDestroy() and don't call on SaveInstanceState explicitly, then too i am not getting any desired effect.......
@Override
public void onDestroy()
{   Bundle savedState = new Bundle();
onSaveInstanceState(savedState);//because of this line....the app is crashing......
super.onDestroy();
    }

but it didn't help..... and in on create i am putting a check if saved instances is null or not....create view accordingly...(i.e from saved instances or from fresh) but it didn't help... also this line is giving a crash ...onSaveInstanceState(savedState);

even if i don't override ondestroy() and kill the app manually from task killer,and then try to open activity B,then too the saved instances thing should work right,because the method OnSaveInstanceState will be automatically called then,right???please help

Ankit Srivastava
  • 354
  • 1
  • 5
  • 24
  • `onSaveInstanceState(savedState)` is a overriden method of activity class why are you calling it explicitly – Raghunandan Nov 14 '13 at 19:55
  • read the full thing please....even if i don't call it...it will be called automatically when the task killer kills the app right????so in that case too why is the view not being displayed(restored)??? – Ankit Srivastava Nov 14 '13 at 19:57
  • @Raghunandan and in this e.g...in the checked answer..the guy is calling it explicitly http://stackoverflow.com/questions/10492637/android-saveinstancestate-understanding – Ankit Srivastava Nov 14 '13 at 19:58
  • you need to read the docs and understand http://developer.android.com/guide/components/activities.html. check the topic saving activity state – Raghunandan Nov 14 '13 at 19:59
  • Dude i have read it all.....please can you help??how can i restore the activity state in my case???can u help me with that??? – Ankit Srivastava Nov 14 '13 at 20:01
  • @Raghunandan even if i don't override onDestroy i am not getting the desired effect... – Ankit Srivastava Nov 14 '13 at 20:06
  • please check the docs especially the note part Because onSaveInstanceState() is not guaranteed to be called, you should use it only to record the transient state of the activity (the state of the UI)—you should never use it to store persistent data. Instead, you should use onPause() to store persistent data (such as data that should be saved to a database) when the user leaves the activity. – Raghunandan Nov 14 '13 at 20:10
  • i would use a service to play the music – Raghunandan Nov 14 '13 at 20:11
  • @Raghunandan i am also using a service to play the music....i haven't stated it here so that the question remains simple...... yeah i understand that onSaveInstanceState() maynot be called when i desire to call it...but i have tried many cases like killing the app manually and a few other(like pressing back button) but the UI never gets updated... – Ankit Srivastava Nov 14 '13 at 20:15
  • @Raghunandan and i am sure that the onSaveInstance is surely called when i navigate away from the activity (by pressing back button) – Ankit Srivastava Nov 14 '13 at 20:18
  • so you need to save the data in `onPause` and restore it in `onCreate`. Why are you absolutely sure – Raghunandan Nov 14 '13 at 20:19
  • Did you read the docs? There's no guarantee that `onSaveInstanceState()` will be called before your activity is destroyed, because there are cases in which it won't be necessary to save the state (such as when the user leaves your activity using the Back button, because the user is explicitly closing the activity). If the system calls onSaveInstanceState(), it does so before onStop() and possibly before onPause(). – Raghunandan Nov 14 '13 at 20:20
  • @i have read it somewhere..maybe in the docs.... i surely read it here http://stackoverflow.com/questions/6678062/when-are-all-the-cases-when-the-onsaveinstancestate-method-called – Ankit Srivastava Nov 14 '13 at 20:20
  • @Raghunandan ok so what is my alternative???if not onSaveInstance what should i do??? – Ankit Srivastava Nov 14 '13 at 20:24
  • @Raghunandan there were mixed opinions over the interent on when it is called ,so i think that is why i was confused...can u tell me how i can restore the activity state in my case? – Ankit Srivastava Nov 14 '13 at 20:27
  • Your storage options http://developer.android.com/guide/topics/data/data-storage.html. You need to save the values in `onPause` and restore the same in `onCreate`. – Raghunandan Nov 14 '13 at 20:29
  • @Raghunandan i have created a new question for this http://stackoverflow.com/questions/19987755/how-to-restore-activity-state-onsavedinstancestate-wont-help – Ankit Srivastava Nov 14 '13 at 20:32
  • ok but how can i save in case of onpause??in case of onSaveinstance i was simply using a bundle and that bundle is automatically passed to onCreate ...but what to in case of onPause??will i have to pass the data and all manually? – Ankit Srivastava Nov 14 '13 at 20:34

2 Answers2

1

From Android documentation:

When your activity is destroyed because the user presses Back or the activity finishes itself, the system's concept of that Activity instance is gone forever because the behavior indicates the activity is no longer needed. However, if the system destroys the activity due to system constraints (rather than normal app behavior), then although the actual Activity instance is gone, the system remembers that it existed such that if the user navigates back to it, the system creates a new instance of the activity using a set of saved data that describes the state of the activity when it was destroyed. The saved data that the system uses to restore the previous state is called the "instance state" and is a collection of key-value pairs stored in a Bundle object.

To fix second problem you must restore data form some other source (service that plays music, restore info from application context or singleton, etc.)

For more info check this.

XeNoN
  • 694
  • 6
  • 16
  • It is simple, you can not restore activity with instant state if you explicitly destroyed it, and you did it by pressing back button. In that case forget bundle and save you data some wear else and that restore it in `onCreate()` method. If this isn't answer you looking for, then update your question, because nobody understand it... – XeNoN Nov 14 '13 at 20:22
  • ok your comment was my answer......that "it won't help because it is explicitly destroyed" i have accepted your answer...and here is a modified version of the question http://stackoverflow.com/questions/19987755/how-to-restore-activity-state-onsavedinstancestate-wont-help – Ankit Srivastava Nov 14 '13 at 20:39
  • @user2993677 that is a duplicate flagged it – Raghunandan Nov 14 '13 at 20:41
  • @Raghunandan the content is duplicate , the question has changed.... the new question is how to restore activity state from onPause?? – Ankit Srivastava Nov 14 '13 at 20:43
1

Basically if you have pressed a back button,restoration of activity should be done using shared preferences/or a database but if you haven't pressed the back button and then you want to restore the state of the activity (because the activity was destroyed by the system ) then the bundle savedinstances can be used ...

Gru
  • 2,686
  • 21
  • 29