How to save State of activity to continue where user left of? - android

How do you save an instance state of an activity. For instance a user opens up a application and clicks a button. A new view is formed and the user types in some information and moves to the next screen in that activity. After that goes back to the main menu of the app. how do you save where the state was before the user went back to the main menu.
Thanks in advance everyone!

http://developer.android.com/reference/android/app/Activity.html
In addition, the method
onSaveInstanceState(Bundle) is called
before placing the activity in such a
background state, allowing you to save
away any dynamic instance state in
your activity into the given Bundle,
to be later received in
onCreate(Bundle) if the activity needs
to be re-created.

Related

activity state when pressed back button vs rotation

I have an object that implements Parcelable so that when the screen is rotated, the data is deserialized and display on screen. However, when I press the back button and click on activity "A" again, the data isn't there anymore. Why is onSaveInstanceState null after you press back button?
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.
onSaveInstaceState will have value only if the activity is Recreated by the system.
Check Recreating an Activity for more info.
When a user knowingly leaves the Activity, the onSaveInstanceState is not called. It is called when the system destroys and recreates the Activity.
For this reason, you are getting null in onRestoreInstanceState.
Refer -
http://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState%28android.os.Bundle%29

Saving and restoring Activity State when Activity is closed by pressing back button by user

I am working on an App for collecting Customer Details like personal, business, assets etc.
The app shows a ListView with options and based on options Activities are started. After entering details in personal user presses back button and returns to MainActivity (Activity with a ListView) and selects another option like business details and assets detail. User enters the details and come back to MainActivity by Back button and selects another option.
I tried to save my Activitys using onSavedInstance(Bundle) and onRestoreInstanceState(Bundle) and sharedpreference but failed.
Can anyone help? I will provide code if required.
OnSaveInstanceState()/OnRestoresInstanceState() is not supposed to be used on a back key pressed from an Activity. In fact , OnSaveInstanceState() is never called. Here the user is explicitly destroying the Activity and removing it from the back stack.
These two methods are used in case where user presses home button or in cases where Android System destroys your Activity.
The options you can try
Try using StartActivityForResult()/OnActivityResult() methods. Here the target activity can set data whcih can be accessed by your parent activity.
Use an application class(a single instance class (singleton class) for holding data).
Things like shared SharedPreferences, files, database, etc.

Is it possible to save previous activity?

My Android Application has only one activity called MainActivity. When I press the back-button and run my application again, the previous activity will be destroyed. Now I want to know that is it possible to save the exact previous activity?
Thank you
Pressing the back key kills your current activity. A user would not expect it to be saved as there is no way to go "Forward" .
You can save the important parts of your activity in SharedPreferences or a database when you press back and restore it to its previous state when the activity next starts which would give the impression that it was "saved"
Activities are created in a stack, when you create A from B then B is now at the top of the stack, when you press the back button you are telling android that the top level of the stack (current activity) is no longer needed and it gets removed.
You will have to save state yourself and restore it if that's what you need to do when B is recreated.
one way you can use sharedpreferences for saving state of TextView,EditView ...etc.and resigning again when activity recreated.
Thanks.maybe helpful :-)

help calling an Intent / activity that has already been created

I have two buttons on my application. One button starts a new Game (lets say solitair).
When ever this button is pressed it will always start a new game
My problem is that I would like a second button to go back to the game that has already started if the user clicks back.
How do I go about recalling that activity that has already been created
Thanks
I assume that the game is from a third party. As I see it you don't even need second button. Just click the first one again and you will get back to the game.
If the game intercepts Back it may intentionally destroy it's state so there is no way to jump back to the place that you were before hitting Back.
You have to save the state of the activity
onSaveInstanceState() and onRestoreInstanceState() are used to handle the activity state
onSaveInstanceState() method gets called for an activity when user leaves the activity. Note this method is only called when activity is present in the History state and user can come back to the activity.
onRestoreInstanceState() restores the state of the views.
see the following link
How to manage activity state
You have to read manual about activity stack
Your application can has more than one task and more than one activity back stack

onSaveInstanceState when click back button in Android

I have an activity that is called at runtime in an Android App. When the user clicks the back button the Activity is destroyed and I overrode the onSaveInstanceState method to save outState to be retrieved in a second time but onSaveInstanceState is never called when the user clicks the back button.
Do you know why this happens?
onSaveInstanceState() is not supposed to be called when the user presses BACK.
It will be called when the user changes configuration (e.g., rotates the screen), if Android believes that the activity is at risk of being destroyed while still being reachable on the stack, and perhaps a few other cases.

Categories

Resources