Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to distinguish between an Android Activity onResume from the home screen?

Tags:

android

Is there any way to tell whether an Activity is being resumed (i.e. onResume is called) from the home screen/launcher?

For example, if I have an Application with two activities, A and B.

Scenario 1: Some user action on Activity A will invoke Activity B, bringing it into the foreground - moving Activity A into the background. As Activity A moves into the background, it goes through onPause() and onStop(). The user (now on Activity B) either finishes the Activity or hits the "back" button, bringing Activity A back to the foreground, causing an onRestart(), onStart(), onResume() sequence.

Scenario 2: If the user hits the "home" button while Activity A is in the foreground and then re-invokes the Application from the launcher, it goes through the same lifecycle as in Scenario 1. I.e. User hits the "home" button. Activity goes through onPause() and onStop(). User launches the application again, causing Activity A go come back into the foreground, again going through the same onRestart(), onStart(), onResume() sequence as in Scenario 1.

As far as I can tell, the Activity has no way of knowing how it was resumed, it just knows it is being brought back into view. In fact, I have a feeling that there isn't really as much of a concept of an "Application" in Android - in the sense of something that has a single entry and exit point.

like image 684
H.Y. Avatar asked Jan 25 '11 02:01

H.Y.


People also ask

What is the difference between onStart and onResume?

onStart() -> called when the activity becomes visible, but might not be in the foreground (e.g. an AlertFragment is on top or any other possible use case). onResume() -> called when the activity is in the foreground, or the user can interact with the Activity. Save this answer.

What is onResume Android?

onResume() When the activity enters the Resumed state, it comes to the foreground, and then the system invokes the onResume() callback. This is the state in which the app interacts with the user. The app stays in this state until something happens to take focus away from the app.

What's the name of the android activity that is invoked if the activity isn't visible to users any longer?

If the activity stops being visible to the user, the onStop() method gets called. After the onStop() method has run, the activity is no longer visible. If the activity becomes visible to the user again, the onRestart() method gets called, followed by onStart() and onResume().


3 Answers

in Scenario 2, your activity will get an onNewIntent call, with the launcher intent passed to it.

like image 87
superfell Avatar answered Oct 12 '22 12:10

superfell


You could capture the back button press on Activity B and pass an extra value to Activity A. If there is an extra value then the activity was resumed from pressing back on Activity B, if there is no extra value then the Activity was resumed from being hidden.

like image 44
Phobos Avatar answered Oct 12 '22 11:10

Phobos


Could Acitivity A use startActivityForResult() to start Activity B and use onActivityResult() to detect that Activity B finished?

like image 40
Bert F Avatar answered Oct 12 '22 12:10

Bert F