onPause / onRestore with savedInstanceState
Asked Answered
C

2

7

I'm pretty new to android development and I need some help saving the state of an activity. What is the correct way to save the instance from onPause and restoring it from onRestore since obviously Android isn't sending the savedInstanceState Bundle as it does with onCreate or onSaveInstanceState for example. Or is there a better way to save other than using the savedInstanceState bundle?

Does this make sense?

[edit] Ok, i think i know what my real problem is... But first, I think what I was looking for was to use SharedPreferences instead of savedInstanceState.

So, doing more debug log watching I'm noticing that instead of bringing the Activity to the top of the stack it's creating a new one. Yes, I realize I'm creating a new one....

         Intent itemintent = new Intent(MediaList.this, AudioPlayer.class);

         Bundle b = new Bundle();
        //...putString some strings to send
         itemintent.putExtra("android.intent.extra.INTENT", b);
         itemintent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
         startActivityForResult(itemintent,0);

...But isn't FLAG_ACTIVITY_REORDER_TO_FRONT supposed to stop it from creating a new activity? I'm guessing it thinks it has to create a new one since i'm sending along some strings?

Better yet, how can I check if the activity is already in the stack and switch to it as long as the strings are the same? -- I'm starting this activity when the user clicks a media item from a listview. [/edit]

Cotangent answered 25/4, 2011 at 23:6 Comment(0)
P
3

For some reason this is not documented in very very bold neon flashing letters, and took me a while to discover, but you don't need to worry about whether your activity exists or not if you simply create it with the android:launchMode=["multiple" | "singleTop" | "singleTask" | "singleInstance"] property set to "singleInstance".

Then there is only ever one instance of it, and your memory fields are preserved as long as the activity hasn't been garbage collected.

The other thing you can do is to create an Application (extended from Application) and store all your persistent objects up in that... it is created first and destroyed last as far as your entire app's life cycle is concerned (including all your activity-less services).

Just create an Application class and specify it in your manifest like this:

<application android:icon="@drawable/icon" android:label="@string/app_name"
    android:name=".MyApplication">

Then if you still REALLY want to save your values for situations where the app is going to be closed, just use SharedPreferences.

Polymerize answered 26/4, 2011 at 1:17 Comment(5)
Thanks for the response. Gave me high hopes but it's still creating a new activity after adding android:launchMode="singleInstance" to the manifest file. It shouldnt call onCreate() again right? x_XCotangent
Now this is interesting... Does the phone's back button kill the activity? Because if i hit the home button and go back to the app it works correctly.Cotangent
Yes bwoogie, the back button kills the activity (so no saving/loading/whatever will bring it back), while the home button sends it to the background. This was not very intuitive to me at the beginning either.Brewster
that's correct, it will only call onCreate once on an activity that is set to singleInstance. onResume is the last thing called before your activity is displayed. It's called every time (including after onCreate)Polymerize
+1 and I would like to add in very very bold neon flashing letters: The default launchMode is "standard".Pakistan
B
1

What is this onRestore method you speak of? It is not part of the Activity lifecycle... I will suppose you mean onRestart. Anyway, the reason you don't get a bundle for onRestart is because you don't need one. Your activity hasn't been officially "killed" so you don't need to restore from a saved state. Your activity was paused, but not removed from memory, so the system is just telling you that it was made visible again. You probably don't need to do anything for this sort of transition event.

Other than that, the way to do it is to save anything you consider a "state" value in the onSaveInstanceState(), then restore them in onCreate. After that you can restore any view-specific properties either on the onCreate itself, or later in the Activity lifecycle (e.g. onResume).

Brewster answered 25/4, 2011 at 23:32 Comment(6)
If he was talking about onPause, I would bet he meant onResume instead of onRestore.Arlyne
Yes, I meant onResume not onRestore. Sorry.Cotangent
Ah, point. I think the same principle still applies. It's more for you to know that it happened than for you to save anything. If you are dealing with a database/content provider, the documentation suggests you save your data to it onPause, but there's nothing to do related to the activity's state.Brewster
Maybe i should have made it more clear what I'm saving. Its actually the current playing audio file from an online xml file. I need check, if the user leaves the activity (while the audio is still playing) and comes back, if they selected a different file or to continue playing the current one and update the player accordingly.Cotangent
I understand @Cotangent problem, and I was looking into the solution until I got detracted. @dmon: onPause needs to be written so that if Android is in need of resources it can take away memory from the original program. You can't assume the info gets put into a secured state...Hassle
can you give me an example how to save the Bundles at onPause()?Soundproof

© 2022 - 2024 — McMap. All rights reserved.