Is there a method of the Activity lifecycle which is called if the user presses the back button, but not if the the method startActivityForResult() is called? I couldn't find a method by testing it.
I'm sorry, I just didn't saw the solution, but here it is:
By calling startActivityForResult(), the methods onPause() and onStop() of the original activity are called.
When pressing the Back Button, the methods onPause(), onStop() and onDestroy() are called.
So the difference is the onDestroy() method.
Normally, it will be onResume() followed by onActivityResult().
However it's possible, though unlikely, that the calling activity will
have been killed at some point while the user worked with the other
activity; this happens when the system runs out of memory, at which
point it starts killing stuff, starting from the 'most inactive'. In
that case, I imagine it would go through onCreate(), onStart(),
onResume() and then finally onActivityResult().
https://stackoverflow.com/a/2869832/323696
The answer I quoted above is correct, except for the explanation for when the calling activity is killed, or finished(), before the called activity completes.
In that case, when the calling activity, Activity #1, resumes after having called another activity, Activity #2, using startActivityForResult, the method onActivityResult in Activity #1 is called BEFORE onResume.
This is important to know if you are instantiating your SQLite Database objects from within onResume in Activity #1. If so, you will also need to instantiate the object from within onActivityResult, when returning from Activity #2.
For more information, read about the 'startActivityForResult method' at http://developer.android.com/reference/android/app/Activity.html.
I haven't been able to find a LifeCycle Diagram depicting this. The step for when returning from an activity for result is always summed up within a text description in the LifeCycle images, stating 'User Returns to the activity', or 'The activity comes to the foreground.'
Related
I've searched through dozens of Stackoverflow posts and the android doc but just couldn't find the answer.
According to the accepted answer of this SF-post the onCreate method runs when the activity is first created. It also notes that in here views are supposed to be created and list data is being binded.
Then the onStart Method runs but here's the issue. Where's the difference? If you do everything inside of onCreate, switch activities, your app will still display the same data, regardless whether you put the app in the background or switched activities.
So if you declare views in onCreate, what do you do in onStart? initiliaze the views to their R.id.view ? Fetch data?
onResume I suppose is then used for listeners since it's the gas and brake according to this SF-posts accepted answer.
onCreate() is called when the activity is first created. onStart() is called whenever the activity becomes visible, which includes when it is first created (after onCreate()) and after it is coming back to the screen from being stopped (e.g., another activity took over the screen).
So:
Put code in onCreate() that needs to happen when the activity is created (and use onDestroy() to clean it up)
Put code in onStart() that needs to happen either when the activity is created or when the activity returns to the foreground (and use onStop() to clean it up)
Frequently, we do not do anything special when the activity returns to the foreground, in which case you do not need to worry about onStart() or onStop().
I have an android activity. If the user switches off the monitor of the android device for some time and then switches back on again, It seems like that the current activity is restarted. I found an activity lifecycle picture on the Internet but it does not tell me what method is called when the user switches off the monitor. It only tells me that when the activity is running, and "another activity comes in front of the activity", the onPause() method is called. But a "black screen" is not "another activity", right?
So what is called when the device's monitor is switched off?
onPause will be called first, then onStop will be called
Activity lifecycle methods and state
onResume() called- Visible
onPause() called - Partially Visible
onStop() called - Invisible
If app is in memory, then onRestart call when app starts again, otherwise onCreate() called
Context
I'm one of the developers working on a pretty large and very complicated project. Half of the project is written with JavaScript and the other half with Java, moreover about 30% of the base product comes to us pre-compiled, so we cannot exactly look inside or debug through it.
Problem
When our application comes back to foreground the activities stack gets destroyed and only the root activity is displayed. I cannot track down why.
Things I've tried so far
Setting a breakpoint on every finish() call I could find, none of them gets called
Setting android:excludeFromRecents="false" to every activity
Removing android:launchMode="singleTop" from the AndroidManifest
I've set a breakpoint in onDestroy() method for the second activity and I can see that "isFinishing()" method is executed to "True", so I know that it is not the OS that is destroying the activity, but there is no information (as far as I can see) which would pinpoint me to which class/property/method caused the method to be called. There is nothing usefully I can see in the stack trace. There is nothing useful I can see in the logs.
I've also tried asking people around in my company, but no answer this far.
Question
Are there any strategies to find what caused a certain lifecycle method to be called?
Update 1
Thank you for the great questions and the help in the comments section. Here is the sequence diagram to explain better what is happening.
Application is launched
Activity A calls onCreate()
Activity A calls onResume()
I launch Activity B
Activity A calls onUserLeaveHint()
Activity A calls onPause()
Activity B calls onCreate()
Activity B calls onResume()
Activity A calls onStop()
I put the application into the background
Activity B calls onUserLeaveHint()
Activity B calls onStop()
I return the application to foreground
Activity B calls onDestroy()
Activity A calls onRestart()
Activity A calls onResume()
Update 2
Here are the screenshots of the memory usage
When I launch the application
When I launch Activity B
When I send the application to the background
When I return to foreground
And the solution is found!
Turns out that my root activity had the following property set:
android:clearTaskOnLaunch="true"
Here is a quote from the documentation:
android:clearTaskOnLaunch
Whether or not all activities will be
removed from the task, except for the root activity, whenever it is
re-launched from the home screen — "true" if the task is always
stripped down to its root activity, and "false" if not. The default
value is "false". This attribute is meaningful only for activities
that start a new task (the root activity); it's ignored for all other
activities in the task. When the value is "true", every time users
start the task again, they are brought to its root activity regardless
of what they were last doing in the task and regardless of whether
they used the Back or Home button to leave it. When the value is
"false", the task may be cleared of activities in some situations (see
the alwaysRetainTaskState attribute), but not always.
Suppose, for example, that someone launches activity P from the home
screen, and from there goes to activity Q. The user next presses Home,
and then returns to activity P. Normally, the user would see activity
Q, since that is what they were last doing in P's task. However, if P
set this flag to "true", all of the activities on top of it (Q in this
case) were removed when the user pressed Home and the task went to the
background. So the user sees only P when returning to the task.
If this attribute and allowTaskReparenting are both "true", any
activities that can be re-parented are moved to the task they share an
affinity with; the remaining activities are then dropped, as described
above.
And here is a great article which helped me to find it: http://developer.android.com/guide/components/tasks-and-back-stack.html
Not sure if this will work:
#Override
public void finish() {
super.finish();
Log.d("derp", "who is calling me?", new RuntimeException());
}
I have some floating bugs in my app, which unable to reproduce clearly. I suspect them from inproper work of my SaveInstanceState|restoreInstanceState mechanism, so I need to check case, when activity is being stopped when goes to background, and recreating after I press back button from spawned activity. Is there a way to force android stop and destroy activity which went to background? It should remain on activity stack, so I cannot just finish it.
Just enable the developer option "don't keep activities" (or whatever its called). This won't remove the activity from the stack, but will actually call onStop() and onDestroy() whenever the user leaves the activity and opens another. When the user presses BACK, Android will create a new instance of the activity, and call onCreate() and onRestoreInstanceState()` as expected.
Looks like it's a copy of this and this questions. Override onPause() method and call method finish() in it.
I have a main activity which calls a child one via
Intent I = new Intent(this, Child.class);
startActivityForResult(I, 0);
But as soon as Child becomes visible the main activity gets its onStop and immediately after that onDestroy method triggered. And as soon as I call finish() within the Child activity or press the back button, the Child activity closes and the home screen shows (instead of the main activity).
How can I prevent the main activity from being destroyed? :\
If you launch a child Activity from which you expect return data, you'll probably want to use startActivityforResult instead.
You may want to check this question: Child Activity in Android as it seems to be the same problem.
Edit:
As for what's happening here, you could place code in the onStop() and/or onDestroy() methods to investigate - at least a call to isFinishing() to check why the Activity is being destroyed.
You should also use adb logcat from your host machine to check the logcat in case it holds more information - and maybe use Log.d() (the result goes into logcat as well) instead of toasts to make sure you don't miss them.
I used Dialog instead of an Activity and everything worked well so I'm leaving it like that.
check androidmanifest nohistory=true and that made the OS destroy the activity before the result. that might be one of the reason for your problem.