I am still learning the ins and outs of Android development. I am playing around with the Notepad tutorial application to try and get different behavior.
Right now, I want to have the application do the following in the NoteEdit activity:
1) If the Back button is pressed, current state is ignored; basically, it's like an implicit cancel, and you are taken back to the list.
2) If the Home button is pressed, it takes you to the home page as normal. However, if you open the application again, it should go back into the NoteEdit activity in the same state as when you left (IE, if you were partway through an edit, for example).
I removed the "saveState" stuff from onPause, because I don't want to store to the DB unless "Confirm" is pressed (instead, I moved the call to saveState to the confirm button). By doing this, hitting "Back" basically throws out your changes, which is what I want. However, going Home and coming back also throws out your changes, though it does remain in the NoteEdit activity. Both "Back" and "Home" cause the onPause message to trigger, and both cause onResume to trigger (either from clicking on the item in the "Back" case, or by going back into the app in the "Home" case).
Is there a way to have these two events handle saving the state differently? Is it possible to have the Home button store the state (temporarily), while not having the Back button do it?
Thanks in advance!
You need to define an onSaveInstanceState method, but instead of saving to the DB (as in the Notepad sample), save your Activity's state to the Bundle. You then need to recover from the saved state in your onCreate when the passed in Bundle is non-null.
Related
I have a Voip calling app using kotlin and when call received, it will open an activity called IncomingView. So far so good. But I want to Minimize the activity on back button and can navigate to other pages and shows a green bar or indicator at the top bar (similar to WhatApp/Telegram) that indicates that the call is going on and when i tap there, it should bring back my "IncomingView". (I know and already implemented that i can create a notification and tap on it brings back my activity)
Any Ideas?
But I want to Minimize the activity on back button
you cant minimize Activity, you can close it. You can "minimize" a fragment in a activity by removing it and later on adding it back - this way its state will not be lost.
If you need to minimize an activity, then I suggest to save its state, then minimize it (call finish() on it), then when you need it back then open it again and it should read its old state and recreate in a same way as it was previously.
I've two activities, A and B.
My app goes from A to B. (A -> B).
When I'm on B and press the back button (hardware back button) the state and UI of A is restored successfully (onResume() is being called).
The problem is, when I press the home button (Actionbar arrow) the previous Activity A calls onCreate() so its state and UI won't be restored as with back button press.
Why is this happening? How can I solve it?
It seems you create a new instance of Activity A when you hit the up button on the ActionBar, instead of going back to the already existing Activity A.
You should override the listener that is invoked when you press the button and call finish() on Activity B instead. So the behavior will be the same as pressing the device's back button.
http://developer.android.com/training/basics/activity-lifecycle/recreating.html
Google has a great discussion on the Developers website. Anytime you leave the app you must save information needed to recreate the last state of Activity A because the system can remove your app from the cache at anytime after it closes. So save important db keys etc... into a file on the system with those keys needed to reload the activity. This will also handle orientation changes for you. The important lifecycle methods are onPostResume to restore state and onStop and onDestroy to save state.
Read about activity lifecycle too.
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.
I'm making an app that has just one Activity. When I press the Home or Recent Apps button, the state is correctly saved and when I go back to the app everything is there. When I press the Back button and return to the app later, savedInstanceState is always null in onCreate. I've read that the state is not saved when the back button is pressed, but this seems unintuitive in my case considering that the back and home buttons are equally valid ways to leave the app in the user's eyes, until they try and go back to what they were doing. Is there no way to save state when the back button is pressed?
If I must do some persistent storage, then:
a) How do I override the back button behaviour to make it save the app when back is pressed, and
b) Can I persistently save a Bundle somehow so that I don't have to duplicate the code for state saving?
What you are searching for is SharedPreferences I think take a look at it. It will help you solving your problem
I may be wrong, but my understanding is that savedInstanceState only saves your data when you change your device orientation, but as soon as you click back button, your activity is destroyed. In order to save your data when back button is pressed, you could save your data by using SharePreferences or you could use singleton class.
http://developer.android.com/reference/android/app/Activity.html take a look at an activity lifecycle first. Sounds to me like you need to override the ondestroy method and persist your stuff.you can use a bundle for simple stuff but complex stuff it's best to use parcel.
The oncreate method should check stuff to see if it's null
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