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

onresume android
onstart vs onresume
android lifecycle
onsaveinstancestate
fragment lifecycle
android application lifecycle
android save activity state on back button
onstart 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.

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

Understand the Activity Lifecycle, An Activity is an application component that provides a screen with Storage use cases and best practices a core set of six callbacks: onCreate() , onStart() , onResume() to learn how to make your dependent components lifecycle-aware​. You can distinguish between these two scenarios with the  So the straightforward answer to the initial question is probably: no. Launching an activity from the home screen through an icon, or resuming it from the recents screen can not be observed from the intent it is (re)started/resumed with.

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.

Handle Activity State Changes, An Activity is an application component that provides a screen with This document describes some common cases in which such transitions happen, and how to onPause() on app A, and onResume() on app B. It switches between Note: When the user taps the Overview or Home button, the system  As a user navigates through, out of, and back to your app, the Activity instances in your app transition through different states in their lifecycle. The Activity class provides a number of callbacks that allow the activity to know that a state has changed: that the system is creating, stopping, or resuming an activity, or destroying the process in which the activity resides.

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

4. The Activity Lifecycle: Being an Activity, So far you've seen how to create activities, and made one activity start Android sees that the screen orientation and screen size has changed, and it destroys the activity, including any variables Q: Why should that make a difference? A: The activity lifecycle methods onCreate() , onStart() , and onResume() get called. Would Activity.onWindowFocusChanged(boolean hasFocus) be useful here? That, plus a class-level flag, something like isFocused that onWindowFocusChanged sets, would be an easy way to tell at any point in your activity if it is focused or not. From reading the docs, it looks like it would properly set “false” in any situation where the

So the straightforward answer to the initial question is probably: no. Launching an activity from the home screen through an icon, or resuming it from the recents screen can not be observed from the intent it is (re)started/resumed with.

Depending on what you are trying to achieve there are some approaches though:

  1. what @superfell suggested:

Check for whether the onNewIntent-method is called on your activity to decide if it was restarted from the launcher. As a precondition you need to set your activity to singleTask/singleTop launchMode in your Manifest:

android:launchMode="singleTask"

depending on what you're trying to achieve, this might be enough! But additionally you might have to deal with what happens when the user presses the back button. Default behavior is to finish & destroy your activity. Thus a brand new copy of it would be launched when it gets selected from the recents screen. Though onNewIntent would not be called, everything would be rebuilt from scratch. If you need to prevent this you can use:

override fun onBackPressed() {
    moveTaskToBack(true)
}

finally when you navigate "back" from an activity you launched yourself onNewIntent will also not be called. If you further need to distinguish why your activity is resumed, you might want to start the 2nd activity with startActivityForResult so the onActivityResult is called when you get resumed.

  1. Launcher activity & Intent extra

A completely different approach would be to have a "launcher" activity in your manifest that directly calls your "main" activity and finishes itself. When calling your main activity you can put an intent extra that allows your main activity to distinguish if it was just launched for the first time, or not. As the extra would be present on further onResumes, make sure to overwrite it the first time you "consume" it:

override fun onResume() {
    super.onResume()
    val firstLaunch = intent.getBooleanExtra(FIRST_LAUNCH, false)
    intent.putExtra(FIRST_LAUNCH,  false)
    if (firstLaunch) {
      // do something
    }
}

and when starting from your "launcher" activity:

intent.putExtra(FIRST_LAUNCH, true)
startActivity(intent)

Beginning Android 4 Games Development, A paused activity can be killed by the Android system at any point in time (for when a user presses the home button to go to the home screen temporarily. case, it is preceded by a call to onRestart(). Activity.onResume(): This is called after  If it is a new instance of the covered activity that comes to the background, the system does not call onRestart (), only calling onStart () and onResume (). Note: When the user taps the Overview or Home button, the system behaves as if the current activity has been completely covered. User taps Back button

Beginning Android Games, So far you've seen how to create activities, and made one activity start another … When Android starts an activity, it calls its onCreate() method. parts of the activity lifecycle (and a little bit in between), and you've seen how to The user navigates to the device's home screen so the Stopwatch app is no longer visible. For example, an explicit request might tell the system to “Start the Send Email activity in the Gmail app". By contrast, an implicit request tells the system to “Start a Send Email screen in any activity that can do the job." When the system UI asks a user which app to use in performing a task, that’s an intent filter at work.

Android For Beginners: Part II, In the onCreate() method, we simply create a TextView for outputting any error messages, as always. In the onResume() method, we simply start the MediaPlayer (if creating it was successful). out how it reacts to pausing and resuming the activity by either locking the screen or temporarily switching to the home screen. Q 13 - What is the difference between margin and padding in android layout? A - Margin is specifying the extra space left on all four sides in layout B - Padding is used to offset the content of a view by specific px or dp C - Both A and B are correct D - None of the above Q 14 -How many sizes are supported by Android? A - Android supported all

Android - Activities, Well the short answer is that there is no difference between dp and sp . font sizes, so they will be adjusted for both the screen density and user's preference. Managing Android Activity Lifecycle by a developer using The main difference between onStart and onCreate is that onStart follows onCreate . Android :: Way To Carry Data Over Between OnPause & OnResume / Used SharedPreferences May 30, 2010. I'm having a hard time figuring out the best way to pass simple values from onPause and onResume in the Android activity lifecycle.

Comments
  • Ah. Actually, I'm trying to track the number of Application launches - but so far haven't found a way to distinguish between the different onRestart() scenarios.
  • Keep in mind there are other cases where an activity will be resumed, besides just the use of the home button. Is it a new application launch when the user gets a phone call, the system kills your app, then resumes it? I think it'll be better to track interactions in your app, rather then the "launching", however that is defined.
  • Yah, I am aware of these other resume scenarios. Even the approach I eventually chose ("singleTask") will register an extra "launch" when ever you switch tasks - e.g. making or taking a phone call. This means I'll over-count a bit, but it seems better than under-counting by incrementing onCreate or really over-counting on all onResume() calls.
  • According to the docs, onNewIntent is only called for certain launch modes - "singleTask" and "singleTop". I've only managed to get it working for "singleTask", however, the docs don't recommend that launch mode.
  • This approach seems to work ok for me. I do have to mark the "entry point" Activity as "singleTask", tho'. Thanks!
  • I have a feeling I'll need to handle both cases (Activity B returns with a result and "User presses back button on Activity B).
  • I guess onActivityResult() would occur after all the other callbacks have finished? As I commented above, I'm actually interested in counting application launches which is why I've been hooking into onStart() and onResume(). I've seen other responses suggesting onCreate() but that doesn't fire when launching from the "home" screen. I could decrement the count if something comes back from onActivityResult() but that seems kludgy - maybe unavoidable?