Android :: Robust + Easy Way To Save / Restore Droid App Instance State?
Feb 19, 2010
In Android, you need to implement the following Activity methods so your application can be restored to its previous state if the OS decides to destroy then recreate your activity:
public void onSaveInstanceState(Bundle savedInstanceState)
public void onRestoreInstanceState(Bundle savedInstanceState)
The examples I've seen of implementing these methods is to use put/getBoolean, put/getInt etc. on the Bundle object (i.e. primitive objects only) to save the application state. This seems hugely error prone way to save your state for a start and I cannot see how this scales to storing complex objects without writing lots of code.
What options do I have for storing/restoring state in a robust and easy to implement fashion?
In case it's important, my application (a game) needs to store about 50 objects, which each store maybe 5 float variables and some store references to other objects. I don't particularly want to have to write save/restore methods for every class and subclass (maybe about 15 of these) I use. It would be ideal if I could just stick all my state relevant objects in an object called "state" and then just call save/load on "state" to handle everything.
Is using Java serialization an option? I've heard it's very slow, but is that a problem for save/restoring? Could I just write my data to the SD card? To a database?
View 1 Replies
Aug 24, 2010
I need to save some state when the user leaves my game during game play. Saving the state doesn't seem to be an issue, but I can't figure out how to restore it. The onCreate function isn't called when the Activity is resumed (only when it is first created), so I can't get my state back there. Logically, I would use onRestoreInstanceState, but it isn't being called.
CODE:..........
I see log output from onSaveInstanceState, onResume, onCreate, etc, but I never see a log message from onRestoreInstanceState and savedState is always null. Why isn't onRestoreInstanceState being called, and is there another way to get my state back?
View 1 Replies
View Related
Jun 24, 2009
My application has floating pop up window (Pop up Window) that is successfully displayed on user click. When screen orientation is changed, everything is recreated and I want my Pop up to remain visible. on Restore Instance State is called and my Pop up function crashes whilst calling "show At Location".
View 4 Replies
View Related
Sep 24, 2009
I wonder what is the right way to implement this? Could anybody advice whether my solution is correct?
So:
Activity is created
User clicks on button and dialog is launched (Dialog, not dialog styled activity)
User opens keyboard
we have onSaveInstanceState called where we save that our dialog was opened and all respective dialog input
we have onRestoreInstanceState and here we check whether dialog was shown, recreate the dialog with respective input.
Is it the right approach? or there is smth. that is done by Android and I am doing some redundant actions.
View 2 Replies
View Related
Jan 11, 2010
As a follow on from my question on sharing state between Activities, how can I save the instance state of my Application? Since Application does not extend Activity, there is no onSaveInstanceState method to override.
NB: In advance, this is not a duplicate. Despite its name, How do I save an Android application's state? relates to Activity state
View 1 Replies
View Related
Jul 3, 2010
I created an instance of a custom class RestaurantList to hold my data (a list of restaurant data received from a web service as json data).How can I save it in onSaveInstanceState? Code...
View 1 Replies
View Related
Apr 27, 2010
I've got an android app that pulls a random 20 questions (rows) as a cursor from a SQLite DB and loops through all the questions to ask the user all 20 questions.
Is there some way to save the cursor's state/location when the activity is paused or stopped so that when the activity resumes the cursor is restored and in the same position as it was when the activity was paused/stopped?
View 1 Replies
View Related
Nov 16, 2010
I'm developing an application which allows user to edit an image and save that edited image as new image on sd card I want to get the current state of the canvas object.
View 2 Replies
View Related
Sep 11, 2010
I have some 34 checkboxes on one Activity which i have to mark and unmark. and I want to save the status of the each checkbox.
I want this persistently. So should i have a sqlite table or preferences ?
If sqlite table is recommended, then what should be the ideal table structure design?
View 1 Replies
View Related
Mar 9, 2010
I cant find a way to save the checkbox state when using a Cursor adapter. Everything else works fine but if i click on a checkbox it is repeated when it is recycled. Ive seen examples using array adapters but because of my lack of experience im finding it hard to translate it into using a cursor adapter. Could someone give me an example of how to go about it.code...
View 3 Replies
View Related
Sep 20, 2010
I have noticed while working in the emulator that whenever i get out of my app via any method other than pressing the back button, my toggle button (if pressed) will have its state saved and return to that state when i return to the app. im guessing its because its value gets saved in a bundle and reloaded when i return. when i leave the app via the back button, this does not happen and the toggle is always off when i return. is there any way to get the value of the toggle back if the user leaves via the back button, without having to save the value in a db table? alternatively, if my apps notification is in the status bar, i can be confident that the toggle should always be selected, so if there is no way to save the state, is there a way to find out if my apps notification is currently being displayed? then it would be easy to just set the toggle each time via a quick check of the status bar.
View 1 Replies
View Related
Feb 1, 2010
I have a MapActivity that contains a MapView with some custom controls, state information, and an ItemizedOverlay composed by some locations that I draw using the default approach (using populate(), super.draw() and createItem()) and by some lines that I draw in the overrided draw() method.
So, when the activity is paused, I have to save:
Some state information
The ItemizedOverlay
[Maybe more Overlays in the future.]
I'm saving the state information as usual, putting them in the bundle. I'm thinking in doing the same with the Overlays, implementing Parcelablein each one of the OverlayItems and so, but I don't know if there is a better way to store the complete state of the MapViews.
The information depends on remote requests that I don't want to repeat each time the activity is paused. Any recommendation?
View 2 Replies
View Related
Jun 27, 2009
How can i save a Bitmap instance to a *.bmp file?Anyone knows? Is it strange that we can only save a bitmap instance to a png or jpg file?
View 7 Replies
View Related
Dec 27, 2009
I'm trying to use OAuth in an Android app. I have it working correctly but have sometimes run into a problem during the authentication phase. In Android, I launch the browser for the user to login and authenticate. Then the callback url will redirect back to my application.
Here is the problem. My application has a OAuth consumer and provider as members of my main class. When the browser is launched for authentication, sometimes my main Activity is discarded to save memory. When the callback url relaunches my main Activity, the provider and consumer are new instances and therefor don't work when I try to make a request to the api. If the main Activiy was not freed during the authentication phase, then everything works correctly because I'm still working with the original consumer and provider.
I tried using onSaveInstanceState() and onRestoreInstanceState(), but haven't been successful. It seems the onRestoreInstanceState() is not called when my callback url is handled. Seems to go straight to the onResume().
What is the correct method for persisting the consumer and provider in this case?
View 5 Replies
View Related
Apr 6, 2012
How I could backup and restore Bluetooth profiles. My goal is to make flashing roms as quick and painless as possible. This is one of last things I have to manually setup after flashing each new rom.
View 2 Replies
View Related
Sep 1, 2010
can someone helpme out with how to save state of the app when the screen orientation is changed i read the development guide at http://developer.android.com/guide/topics/resources/runtime-changes.html#HandlingTheChange
But i dont know how to define the method collectMyLoadedData();
View 2 Replies
View Related
Aug 30, 2010
I am writing an app where I get all the data from the rest call and display all the data in a custom component list(based on Linear Layout) which is added to a LinearLayout. I write this code in onCreate of the activity. The problem is when I switch activity using startActivity, and come back to the calling activity (using startActivity) then onCreate is called again. I see onPause, onStop called when I call other activity. Is there any way that I can save the application's state?
View 1 Replies
View Related
Jul 16, 2010
I am developing an android application in which,the first activity is that for Login. After successful authentication, user can see an activity with 4 Tabs. My problem is that when user taps on the home button,the application should terminate and when the user again starts my application, he should be able to see the Login screen again and after successful Login, user should be able to see the tab on which he was working before application terminated(i.e., save state for all the tabs).
View 3 Replies
View Related
Aug 9, 2010
I've encountered a problem wile using my own subclass of android.app.Application. I have numerous activities in my application and I'm running a custom ROM which consumes quite a lot of memory. What happens is when I launch a third party activity (Camera) my Application is killed. My Application subclass stores vital data for the whole app so it can't work without it. android.app.Application doesn't have any methods for saving or restoring application state. Activity does have them but they are not suitable.
How to perform save/restore state on Application subclass?
UPDATE I've managed to do so by filling a Bundle obtained from onSaveInstanceState and restoring values in onCreate. But is there any better way?
View 1 Replies
View Related
Sep 4, 2010
In a previous question I asked how I could display two different layouts for portrait and landscape orientations:
Problem switching to landscape orientation
Now I would like to know how I can save the state of my application before it goes into another orientation? I seem to lose this data any time orientation changes.
View 1 Replies
View Related
Oct 12, 2010
I was looking at the way Android handles orientation change for my application (I discovered that it restarts the mainactivity on orientation change :| ). I've seen that you can ovveride the method protected void onSaveInstanceState(Bundle outState)to save stuff, then have the in onStart. The problem is that I've my view with custom objects and a listview using a custom adapter... Everything is in a ArrayList of these objects, but I've noticed that you can't put arbitrary objects in the bundle! So how do I save the state?
View 2 Replies
View Related
Jan 22, 2009
In an ActivityUnitTestCase I'm trying to do:
CODE:...............
But on the save state call I get this trace:
CODE:.................................
Going through the ApplicationContext source code I've found this (http://tinyurl.com/d8qsyc):
CODE:...................
It seems the problem is that the activity token is not set. But here I'm puzzled, I don't know where to go next. Any ideas on how to fix it or workaround it are appreciated. I want to test that my application is saving its state correctly.
View 3 Replies
View Related
Jul 1, 2010
I have an Activity, which contains a bunch of check boxes and a submit button.
When the submit button is clicked, the activity will exit (finish() will be called). Before the activity exits, I want to save the states of the check boxes in the activity, so when the activity starts again, the check boxes can stay in their previous states. What's the correct way to save these states? Should I use SharedPreference.
View 1 Replies
View Related
Apr 30, 2010
This is a bit complicated to explain (and my first post), so bear with me please: I am currently trying to figure out the best way of saving my application's state in a bundle on the onSaveInstanceState event (working on a game). My application's state is based on a "world" class which contains various objects. Among these objects there is a creature object which contains bitmaps (Bitmap object)(allowing me to draw the various sprites of the creature when it's walking with more flexibility and accessibility).
Bit of creature constructor Java:
CODE:.................
After looking around for a few hours I found that serializing the world object (and all its sub-objects) was an acceptable way of saving it in the bundle. Unfortunately it appears that we can't serialize bitmaps and considering they're a part of my "creature" I can't seem to see a workaround.
Here come my questions: 1- Am I doing something fundamentally wrong? (not supposed to save bitmaps in objects? supposed to handle bitmaps in a separate class which I don't pass on the bundle and reload my bitmaps when restoring the application? ...) 2- Is there another way of passing my "world" object into my bundle (would using Parcelable work?)
View 5 Replies
View Related
Nov 17, 2010
I am trying to save some values in the onSaveInstanceState(Bundle) method of my activty by following the example here: How do I save an Android application's state?
But it doesnt seem to load it from the Oncreate(). the bundle object is always null but whenever i call another activity, it does indeed go into the onSaveInstanceState method to save my values.
Now i read that question i just posted and someone noted how they could not get it to work in an emulator? unfortunately that is all im working on. On an emulator and cant test the app on the device as i have no device available to me right now And the web services i am interacting with are in a local VM on my work machine that cannot be accesses remotely at the moment.
My question is, is it true that saved bundles dont work on emulators? I also noticed how when i do dismiss or bring up a new activity , the activity calles onPause and then onStop. when i bring back the same activty, it goes straight to onCreate?
now according to the docs here http://developer.android.com/guide/topics/fundamentals.html#actlife That is correct in terms of what the lifecycle diagram shows but if you read below that diagram on the onStop() section it says the next step it goes into is either onRestart() or onDestroy()? no mention of onCreate? Type on the docs?
Anyways here is my onSaveInstanceState() and onCreate() both in the same activity: code...
View 1 Replies
View Related
Sep 29, 2010
I'm new to android, please help me how to save the state of checkbox,
i.e.
I've ListView with checkbox, three textview and again checkbox. I wish to save the checked state of item(s),
how to save the state of checkbox.
View 5 Replies
View Related
Oct 20, 2009
I have a custom View, what is the proper way of saving / restoring it's state after orientation change. I know I should implement onSaveStateInstatnce() and onRestoreInstanceState() methods, but how exactly (with which argumets) and when I should call them?
View 4 Replies
View Related
Nov 24, 2010
It would be nice if StackOverflow had a section where we could post tutorials like mine so that I can answer literally hundreds of questions that have been asked here with a single blow. See... every day I read questions about how to pass complex objects between activities, how to save state so that your app can resume after orientation change, how to update listviews when the data changes, etc, etc. Here is the second part of a tutorial series I posted on my blog... I hope that you actually read it... because I haven't seen any examples like it anywhere... and it has changed how I think about developing for Android across the board. The question is... is there a downside or negative affect of developing like this?
Beyond Smart Lists - How Observable Singletons change the game. Please read through both of these tutorials carefully... I will answer any questions about it here that I can... I really want to know what you think about this and if it might solve issues for you. NOTE TO MODERATORS: there are no advertisements of any kind on my blog.. so don't just close this because you think I am spamming somehow... I am not going to duplicate my post here.
View 1 Replies
View Related
Jun 1, 2009
I have an application which uses a cookie to tie a user to a session on my server.
Is there a simple way to save and restore this cookie to a SQLiteDatabase so that the user does not have to reacquire the cookie every time the app starts?
View 4 Replies
View Related
Nov 7, 2010
As usual in android, each time the screen is flipped to portrait/landscape mode an Activity runs through life-cycle from onSaveInstanceState to onDestroy and then is recreated.In my Activity there's a ButtonText which can be changed by the user. It's reseted to the initial state, but I have to save the last state somehow. How can I achieve that, will I have to override onSaveInstanceState? Can someone show an example?
View 1 Replies
View Related