I'm not talking about restoring application state (using Bundle or configuration change or even SharedPreferences) i'm talking about the fact that i have a certain task, that is a base activity: HomeScreenActivity and from there i can have a series of activities opened for the user's flow.
I want in case of application's death or user killed his own session to restore the whole task, not just one activity, i mean to restore the whole activity stack i had before the application was killed. if the user was in my billing screen and left off my app i want to take him back to that screen, but i want that if he'll press back he'll go back to my search screen or filtering screen, whatever was there before he left off.
I have no problem saving the activity's order in sharedPreferences and know the order once i start my home screen, but i wonder if there's a way to tel landroid to start an activity, which sould only enter the task's stack and not call the activity's onCreate method unless i actually get to it(with back key).
Perhaps someone will have a better idea, but the only thing that comes immediately to mind is to add a "resetup" flag so that you could sort of replay a script of the user's previous activity navigation actions within your app, having your activities start each other up in turn but due to the resetup flag bypassing most of their code so they don't really do anything except essential setup and start the next one.
One complication is that it's probably the activity they were actually in which android will resume.
Related
I have an app, a single activity app with fragments in it.
The usual use case for this app is, that you start it and put the phone away and every now and then, you get back to the phone and insert some data... It's a logging app, you are doing something and insert your results into the app...
I have the problem, that every now and then, my activity get's destroyed and is recreated with an empty bundle... (Most of the time this is not the case, but every now and then this happens...). My app sometimes starts a service, even this service is killed in this case...
This means, that the system has killed my app, does it? How can I avoid this?
I need to keep the user data and the current top fragments... And they are saved to the bundle and everything works as long as their states and the data get saved...
Btw., my activity is always the TOP ACTIVITY, only that the screen turns off often... I just want to keep my activity alive as long as possible until the user leaves it with the back button... Or to save the state reliably
IMPORTANT NOTE
onSaveInstance does not always work (it's not part of the lifecycle and therefore not guaranteed to be called)... it only works most of the time... I need a way to that works always... If android kills my app...
don't keep your app in memory
You don't want to block Android from killing your app. What you want is to restore your app's state properly. Then the user will never notice the app has been destroyed and the user still gets the benefit of an app that was destroyed when not in use.
If you really want this use a wakelock. This will drain your users battery so I think twice before implementing this... Info at How do I prevent an Android device from going to sleep programmatically?
onSaveInstanceState explained
To do so check what information is needed in the bundle and persist that information with the onSaveInstanceState(bundle:Bundle) method so you can reuse it in onCreate(sameBundle:Bundle).
More information available from Google documentation at Save your Activity state and Restore your Activity State.
About Android Activity lifecycle
As stated by #prom85 in the comments below it's not guaranteed that the onSaveInstanceState method will be called because it's not part of the lifecycle. Workaround for this is using the onPause lifecycle hook to ensure your data is stored.
More information at Android: onSaveInstanceState not being called from activity
I had a similar problem, I arrived at this post while searching for a solution, you have to play with the manifest to achieve this and also understand what exactly activity is, in Android eco system,
In Android activity is a task which has a pre defined work.
I dig a lot in the documentation, I found that, we can configure activity in two ways,
Persistent
non persistent
if you mention for the activity in the manifest as
android:persistent="true"
and run the below use case
Start the APP
Press back or home button
you select the activity in the back stack again to bring it to front
Activity enters start -> pause -> stop - > resume , it does not get into onDestroy method.
if do not mention
android:persistent="true"
for the same use case
Activity enters start -> pause -> stop -> destroy, and if you select the activity from the back stack
Activity enters resume->create->start
If you want to run a service/task on activity start which keeps running when the app is in back stack, then you have to start that in the onCreate method, and kill them onDestroy by specifying your activity as persistent in manifest.
I hope my above solution might help others who arrive here for the same problem
I have an offline-online application, i found a strange issue in it, may be it is not, but i did'nt understand about it..
App requirement is that, if internet is available, even from starting app or from resuming, i call webservices and store data in sqlite, otherwise app stays in offline mode,
I have 2 activities, second activity contains an id, that i passes through intent (that point is important),
My Problem:
if i am in second activity, and internet is running, and i press home button , then this 2nd activity pauses, then stop which is a default behavior in android, i goto settings, turn wifi off, then press app icon again to get back in my app, here i got confused, i expect that my app now will be in onResume, but when i see in logcat its onCreated called and app
crashes, nullPointerException occurs, because this 2nd activity does not have that id, i passed through intent..
Note:
If i use recent app button to go to "settings", then come back again after turing wifi off, and repeat all this behavior, then working fine, its onResumes called not oncreate..
My Question
Why it is going in onCreate while i my expectation is to be onResume while i came back from app icon?
The NPE reason is clear, your second activity doesn't have the value and it crashes.
Why do you get different behavior then!?
It's because the launching intents are different. When you "task switch" Android is merely stopping your app but leaving it there (no guarantee) in case you want to switch back.
Going home (with home) is a clear indication that you want to leave the app, and although it will remain in memory and cached (as long as there is available memory), going back through the launcher (or App Icon as you call it) fires the LAUNCHER category (which goes to your Activity 1 first).
Take a look at this StackOverflow question (and answer) to better understand the consequences.
In any case, your problem is that your app must always be designed to resume in an inconsistent state and be able to recover. Android will kill your references, will destroy your variables and most likely send your app to hell overnight even if you have it running… if the phone goes on standby, chances are processes that aren't doing anything will be paused/stopped and likely killed.
Re-design your app so this is not a problem.
You say:
"I have 2 activities, second activity contains an id, that i passes
through intent (that point is important),"
Well, why not make it easier and have ONE activity and TWO fragments? Then use Fragment Arguments to pass the value?
Or why not store the value in the preferences and/or a local database and recover it during onCreate?
And also why not make it so that if Activity 2 doesn't have a value, it calls Activity 1 and closes itself (better than a crash, huh?).
Etc.
As you can see there are multiple things you should consider. All in all, never trust that your app will be alive, because it won't.
Once your activity's onStop gets called it's susceptible to be killed by the android system to collect resources for other apps which is what i think happened in your case.If it is killed, android will obviously call OnCreate when you get back to the activity.Check this for clarification. For experimenting you can try opening more than one apps from your recent apps and then return to your app. It may crash there too now.
You stated that you can see that the activitiy is stopped (onStop) if you go to the settings. That is the behaviour shown in the Android activity lifecycle. The counterpart for onStop is onCreate. So it does what the documentation tells us. Btw activities are paused if they are visible in some way and get stopped if they are not visible anymore. This would explain why your activity get paused. For further information read Managing the Activity Lifecycle. You can find a whole picture of the lifecycle here.
This type of behaviour can be seen when you change some system configurations like font type,font size or language. But turning wifi on/off won't destroy the app and recreate it again. Check http://developer.android.com/guide/topics/manifest/activity-element.html#config for more information
I have got some programs, need help.
I switch my app(have activity) to background(eg:By Home-Key), and use other APP to kill it.
then check it by Using getRunningTasks() and getRunningAppProcesses(), but i can not understand the result : I can find the app's Activity(TopActivty/BaseActivity)in the RunningTasks, but not in RunningAppProcesses
My problems:
1、Home can i remove all Activity when the process been killed?
2、In this case, how can i restart my APP when click the app-icon?
Thanks
can i remove all Activity when the process been killed?
When your process goes away, your activities, services, threads, and all that go away as well.
However, the "task" remains. This is basically a description of the back stack for your app, and any apps launched from it that were launched into your task versus starting a fresh back stack in another task. This description includes things like your saved instance state (i.e., onSaveInstanceState() Bundle), the Intent that was used to start the activity, etc.
how can i restart my APP when click the app-icon?
If the user is returning to your app via the recent-tasks list, ideally you do not "restart" the app, but return the user to where the user left off.
The developer documentation covers various ways that you can control, somewhat, the behavior of the back stack and your app (or individual activities) as they come back into being when the user returns to one of your tasks.
There's a scenario in my app which it could go activity a->activity b->activity a->activity b...infinitely, and eventually it'll get OOM.
Is anybody aware of a way to make it like the "don't keep activities" behaviour,e.g. the activities will be killed to release memory but still in history, so it can be recreated as user navigates back?
This isn't possible. The activities need to be present in the stack for Android to be able to go back to them. What you can do is to keep track of the data that the activities are managing yourself, so that as the user goes from ActivityA to ActivityB to ActivityA you keep pushing a data packet onto a stack that is available to both activities. Then you can use `Intent.FLAG_ACTIVITY_REORDER_TO_FRONT' to transition from one activity to the next.
In this case you will only ever have one instance of ActivityA and one instance of ActivityB, but they should be able to present the user a different view each time they get control by just looking at the data packet on the top of the stack. When the user presses the back button, you should pop the top data packet off the stack and then start the appropriate activity for it (using `Intent.FLAG_ACTIVITY_REORDER_TO_FRONT' to ensure that you don't create a new instance).
I am developing a game for Android and have what I imagine to be a very common scenario, but I can't figure out how to do it.
I have a Title Screen where the user can select "New Game". When they start up a game, I push a GameActivity onto the stack. Then if the user hits the physical back button the phone, the application goes back to the TitleActivity, as I intend it. However, I need a way to allow the user to resume the game at this point, but I don't know how to get back to htat instance of the GameActivity. Any help is appreciated.
You can't guarantee that you will get back to that instance of GameActivity you need to save the game state somehow, and make your GameActivity able to resume from that saved state.
Make sure you understand the Activity lifecycle. The instance of your activity can be killed at any time when it isn't visible, so you need to save state at the proper point (onSaveInstanceState).
The answer I would give is: don't do this.
Finishing an activity (which is what happens by default when you press back) does finish that activity. You don't return to it.
If you want to allow the user to press back to pause your game into a menu, capture the back key with onBackPressed(), and instead of allowing the default behavior of finishing the activity, simply show your menu in-place in that activity. Generally I think for games making the core game UI be one activity that is the base activity launched for the app makes more sense than trying to split it into activities. Games are just special that way.