If I have a method called onStop and it calls super.onStop();. When does this method run?
In my main activity, I start another activity as follows: startActivity(new Intent(this, MyNewActivity.class));. Does this automatically stop my main activity and call onStop?
Also, what kind of things should I do in the onStop method of my main activity. So far all I have done is unregister listeners to free up space but other than that is there anything else I should do?
Thank you, any help is greatly appreciated.
When does this method run?
This method calls when your Activity is no longer visible to the user
Does this automatically stop my main activity and call onStop
Yes when you start another Activity first onPause() function get called and then onStop().
what kind of things should I do in the onStop method of my main activity
It depends on your implemenetation and your needs but persisting data recommended to implement on onPause() callback function
In my main activity, I start another activity as follows: startActivity(new Intent(this, MyNewActivity.class)); Does this automatically stop my main activity and call onStop?
You can know yourself what happens when you call new activity takes focus just put a simple log statement and check yourself.
http://developer.android.com/guide/components/tasks-and-back-stack.html
Quoting from the docs
When the current activity starts another, the new activity is pushed on the top of the stack and takes focus.
The previous activity remains in the stack, but is stopped. When an activity stops, the system retains the current state of its user interface. When the user presses the Back button, the current activity is popped from the top of the stack (the activity is destroyed) and the previous activity resumes (the previous state of its UI is restored).
onStop()
Called when the activity is no longer visible to the user, because another activity has been resumed and is covering this one. This may happen either because a new activity is being started, an existing one is being brought in front of this one, or this one is being destroyed.
Followed by either onRestart() if this activity is coming back to interact with the user, or onDestroy() if this activity is going away.
Check the topic saving activity state
http://developer.android.com/guide/components/tasks-and-back-stack.html#ActivityState
Note: When the system stops one of your activities (such as when a new activity starts or the task moves to the background), the system might destroy that activity completely if it needs to recover system memory. When this happens, information about the activity state is lost. I believe this happens on priority basis.
If you need to store persistent data do it in onPause
For more info check the activity lifecycle
http://developer.android.com/reference/android/app/Activity.html
Related
When you're in an an Activity (we'll call it A), and you invoke a subsequent Activity (B), perhaps as a result of clicking a button in A, and then RETURN to that prior Activity A, either by clicking the Back button or explicitly calling finish() from within B, it causes A to be completely rebuilt, calling its constructor and its OnCreate() method, etc.
Is there any way to prevent that from happening, so that it actually does return to the prior, already existing, Activity A?
Correct me if I'm wrong, but it should not call onCreate() here's a gross over simplification, but let's say activity's are managed much like a simple stack, let's call it AppStack
When a onCreate() for Activity A is called, the OS pushes the Activity Instance onto the AppStack
________ _________________
Activity|
___A____|_________________
When you click a button on Activity A, it launches a new intent to Activity B
Intent actB = new Intent(this, ActivityB.class);
and subsequently puts Activity A into Stopped state
When Activity B's onCreate() is called the OS pushes that Activity Instance onto the AppStack
________ __________________
Activity|Activity|
___A____|___B____|_________
Now if you call finish() or super.onBackPressed() in Activity B, the OS will pop() the Activity from the AppStack
________ __________________
Activity|
___A____|__________________
When the OS returns to the previous activity, it sees that it is Stopped and begins the process of Resuming it through onResume().
Now if there is some data that you require to be persistent, you can add it in by Overriding onResume()
Check out the activity lifecycle docs, for more info:
This is by design:
If an activity is paused or stopped, the system can drop it from memory either by asking it to finish (calling its finish() method), or simply killing its process. When the activity is opened again (after being finished or killed), it must be created all over.
See Activity Lifecycle. It's also why the Service class exists:
A Service is an application component that can perform long-running operations in the background and does not provide a user interface. Another application component can start a service and it will continue to run in the background even if the user switches to another application.
It's not a typical scenario but when onCreate() is called when going back to that activity that means the Android OS kills it in the background.
Reason: Android is experiencing some memory shortage so killing some of the background task will be a must.
Is there any way to prevent that from happening?
No, you don't have a control over it, there are many reasons why its having a memory shortage e.g. other app installed that certain device is consuming more than expected. Although you can handle this use-case by storing the current information in onSaveInstanceState() and recovering the value from onCreate().
Calling finish() on ActivityB or pressing back will just destroy ActivityB.
ActivityA will not be completely rebuilt. This means it will not call onCreate method. It will just call onResume.
This is the normal behaviour.
However, on special situations, the system could destroy ActivityA (maybe because it needs memory to perform another task), so when you go back to it, the system will have to rebuild it.
To simulate this situation, there is a setting that you can check/uncheck, called "Don't keep activities".
If you have it checked, you will be simulating the situation explained above, it will always destroy the ActivityA as soon as it is not shown, and when you come back to it, the system will have to rebuild it calling onCreate.
I wrote a game, and in logs from market i sometimes see following pattern:
06:02:13:835|INFO|1|MainActivity|MainActivity#2.OnCreate
06:02:13:932|INFO|1|MainActivity|MainActivity#2.OnStart
06:02:14:010|INFO|1|MainActivity|MainActivity#2.OnResume
...
06:09:27:688|INFO|1|MainActivity|MainActivity#2.OnPause
06:09:28:895|INFO|1|MainActivity|MainActivity#3.OnCreate
06:09:29:159|INFO|1|MainActivity|MainActivity#3.OnStart
06:09:29:319|INFO|1|MainActivity|MainActivity#3.OnResume
06:09:29:551|INFO|1|MainActivity|MainActivity#2.OnStop
06:09:29:596|INFO|1|MainActivity|MainActivity#2.OnDestroy
MainActivity instance #3 is created and started before MainActivity instance #2 is destroyed. What does this pattern mean ? Why does new instance start before previous has been destroyed ?
Obviously i don't create activity by hand. Users simply start game by tapping icon(at least i hope so).
Activity has singleTask launch mode.
Thank you!
The onDestroy callback is not guaranteed to be called. From the docs:
onDestroy() = The final call you receive before your activity is
destroyed. This can happen either because the activity is finishing
(someone called finish() on it, or because the system is temporarily
destroying this instance of the activity to save space. You can
distinguish between these two scenarios with the isFinishing() method.
Suppose your application has two activities. Then this would be the life cycle
onDestroy will be called if you explicitly call finish(); yourself or like the example if you press Back button because pressing back key actually provokes finish() method on your activity, and it causes your activity to be paused->stopped->destroyed
I have tried to research exactly when the onDestroy method is called for an activity, but I've read some confusing and conflicting information. In general, my question is: under what circumstances is the onDestroy method actually called on an activity? More specifically, if I have two activities, activity A and activity B, if activity A is running and I create an intent and switch to activity B, is activity A only stopped, or is it destroyed?
Like stated in the official documentation:
onDestroy()
The final call you receive before your activity is destroyed. This can happen either because the activity is finishing (someone called finish() on it, or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the isFinishing() method.
In your example the Activity A is stopped and could be destroyed by the System
Note per documentation link above:
…do not count on [onDestroy()] being called as a place for saving data … [see] either onPause() or onSaveInstanceState(Bundle).
onDestroy() is called whenever:
The user takes out the activity from the "recent apps" screen.
The user takes out the activity from the "recent apps" screen.
onStop() is called whenever:
The user leaves the current activity.
So in your example, when the user launches Activity B, Activity A called onStop().
EDIT:
The onDestroy() method is not always being called, according to the documentation. onStop() is always called beginning with Honeycomb, so move code you explicitly need to do before the activity stops to there.
Starting with Honeycomb, an application is not in the killable state until its onStop() has returned.
https://developer.android.com/reference/android/app/Activity#ActivityLifecycle
Hope this helped :D
I am making an application in which i want to let the control go to some activity and then to come back. When I have searched about that problem, I came to know that this problem could be solved by using OnPause() and OnResume(), but I don't know how to use them.
This is the code where I am transferring the control to some other activity. Please tell me how to use OnPause() and OnResume() in this regard.Thanks in advance.
Intent intent = new Intent(MCQ.this,ConfigActivity.class);
int ClassIndex = 2;
intent.putExtra("ClassIndex", ClassIndex);
startActivity(intent);
You are not making too much sense. When you call startActivity(), Android will put your current Activity to the background, and start (or bring to foreground) the new Activity specified in your Intent.
onPause() called when your Activity disappears from the screen, eg. the system either puts it in the background, or terminates it.
onResume() is called when your Activity becomes visible on the screen, eg. either after onCreate() if Android created a new instance of your Activity, or when the system brings your Activity back from the background.
There is no explicit passing of control between Activities. If it is on the top of the activity stack (visible on the device screen), than you can interact with it.
http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
onPause() and onResume() are called by android.
Your job is to implement these methods. onPause and onStop are called at your launching activity by Android, and onCreate, onStart and onResume at the Activity your starting. If you want to go back, you usually just press the back button at your device.
You might want to take a look at the Android devGuide and especially at the Activity lifecycle.
I think you may be confused as to what onPause() and onResume() are used for.
onResume() is called when the Activity is about to show it's view to the user. You can use your intent in this method so the main Activity would instantly go to the ClassIndex activity. However, when you leave the ClassIndex activity, onResume() will be called again which will send you back to ClassIndex.
onPause() is called when the Activity is leaving. Whether this is for locking the screen or moving to another Activity. In this case, when you call startActivity in the main Activity, Android will call onPause() in the main activity before entering the new one.
What you may want in this case is to use startActivityForResult() and use it to determine if you're coming back from your ClassIndex activity or not. If not, then start it. If so, then move on to the main activity.
A better solution may-or-may-not be to start the app in ClassIndex and move to your main activity when the user is done with it. This would be the case if you want to move to ClassIndex every time the user enters the app.
The main activity in my app sometimes calls startActivityForResult, expecting a result that will tell it (the main activity) what information to display next. Looking at the documentation for the process lifecycle, it appears that while the selection activity is active, the main activity is considered a "background" activity and could possibly be killed.
So what happens when the selection activity completes? I see that my activity will be re-created and onCreate is called with the SaveInstance Bundle, but then what? Is onActivityResult then called just as if my main activity had never exited and been re-created?
Also, is there any way to force this behavior in a testing environment, since it should otherwise be a very rare occurrence?
Hint: log statements
The paused state as described in docs is:
If an activity has lost focus but is still visible (that is, a new
non-full-sized or transparent activity has focus on top of your
activity), it is paused. A paused activity is completely alive (it
maintains all state and member information and remains attached to the
window manager), but can be killed by the system in extreme low memory
situations.
That means, under normal circumstances , your main activity should just transfer the control to onActivityResult() when the selection activity completes.
However, docs also state that:
A background activity (an activity that is not visible to the user and
has been paused) is no longer critical, so the system may safely kill
its process to reclaim memory for other foreground or visible
processes. If its process needs to be killed, when the user navigates
back to the activity (making it visible on the screen again), its
onCreate(Bundle) method will be called with the savedInstanceState it
had previously supplied in onSaveInstanceState(Bundle) so that it can
restart itself in the same state as the user last left it.
In such cases, the main activity can be redrawn.
One important point to note is that the docs have never mentioned onActivityResult() as one of their lifecycle methods here
So, it might also be the case where android system treats a sub activity and parent activity (read startActivityforResult() and onActivityResult()) in the same manner as it treats an activity - dialog as stated here:
A visible activity (an activity that is visible to the user but not in
the foreground, such as one sitting behind a foreground dialog) is
considered extremely important and will not be killed unless that is
required to keep the foreground activity running.
The answer is basically "yes": The activity is recreated and the control flows through onCreate(), onActivityResult(), onStart(), and so on.
An activity is also destroyed if the user rotates the device, e.g. from portrait to landscape, unless the application did not explicitly prevent this behavior. So simply rotate the device (CTRL-F11 on the emulator) to test.
Suppose there are two activities A and B and Activity A calls Activity B through startOnActivityResult(intent,200) then your Activity goes to background and Activity B is called (onCreate,onStart,onResume) depends on what you have overridden.
Whenever your B activity calls finish() then your Activity B is destroyed and Activity A comes to foreground. In this case now ie Activity A the call will be onActivityResult -> onStart -> onResume but your onCreate will not be called as it is only called when you call a certain Activity.
Suppose you have not called finish() from activity B and called Activity A through intent then only you onCreate() will be called.
Also onActivityResult() is very useful when you want to retain value of your spinners or you want to notifydatasetchanged() your ListView of your First activity after the event of Third activity. You just have to check your resultCode from the Activities in onActivityResult and perform your actions