Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android lifecycle onActivityResult vs onStop

If activity A starts activity B for result, I was under the impression that the onStop method of activity B is called before the onActivityResult method of activity A. Why is that not the case? I just tested it with a static variable that is set in B.onStop but when I read it in A.onActivityResult the variable is till null.

like image 385
learner Avatar asked Apr 08 '13 15:04

learner


People also ask

What is the difference between onPause () onStop () and onDestroy () lifecycle methods?

Difference between onPause(), onStop() and onDestroy() So, onPause() is logically before onStop(). From onPause() it is possible to call onResume() but it is not possible once onStop() is called. Once onStop() is called then onRestart() can be called. onDestroy() is last in the order after onStop().

What is onStop () activity?

The onStop() function is called when the application enters the stopped state. In this state, the activity is no longer visible to the user. This may happen for the following reasons, the user performing some action that invoked another activity to come to the foreground or the activity finished.

When onStop method is called in Android?

onStop() Called when the activity is no longer visible to the user. Either because another Activity has resumed, and is covering this one, an existing activity is coming to the foreground, or the activity is about to be destroyed.

What is the difference between onPause and onResume?

The onResume() method runs after the onStart() method. It gets called when the activity is about to move into the foreground. After the onResume() method has run, the activity has the focus and the user can interact with it. The onPause() method runs when the activity stops being in the foreground.


2 Answers

I think what you want to do is call onPause, however, you don't want to do too much there. Here is what the docs say:

protected void onPause () Added in API level 1

Called as part of the activity lifecycle when an activity is going into the background, but has not (yet) been killed. The counterpart to onResume().

When activity B is launched in front of activity A, this callback will be invoked on A. B will not be created until A's onPause() returns, so be sure to not do anything lengthy here.

This callback is mostly used for saving any persistent state the activity is editing, to present a "edit in place" model to the user and making sure nothing is lost if there are not enough resources to start the new activity without first killing this one. This is also a good place to do things like stop animations and other things that consume a noticeable amount of CPU in order to make the switch to the next activity as fast as possible, or to close resources that are exclusive access such as the camera.

like image 72
Konsol Labapen Avatar answered Sep 23 '22 01:09

Konsol Labapen


I think your impression is wrong, onActivityResult is called before onResume and the documentation for onStop at http://developer.android.com/reference/android/app/Activity.html in the table following the activity cycle diagram states

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.

Thus onResume in A is called before onStop in B is called.

like image 44
Hoan Nguyen Avatar answered Sep 22 '22 01:09

Hoan Nguyen