Is there a way to distinguish Android Activity onResume from the main screen?

Is there any way to know if an action (like onResume) is resuming from the main screen / launch?

For example, if I have an application with two actions, A and B.

Scenario 1: Some user actions in Activity A will call Activity B, bringing it to the forefront - moving Activity A to the background. When activity A moves to the background, it goes through onPause () and onStop (). The user (now on Activity B) either terminates the Activity or presses the back button, returning Activity A to the foreground, calling the sequence onRestart (), onStart (), onResume ().

Scenario 2: If the user presses the home button while action “A” is in the foreground and then re-calls the application from the launchpad, it goes through the same life cycle as in scenario 1. Ie The user clicks home button. Activity goes through onPause () and onStop (). The user starts the application again, forcing Activity A to return to the forefront, again looking at the same sequence onRestart (), onStart (), onResume (), as in scenario 1.

As far as I can tell, activity has no way of knowing how it was resumed, it just knows that it is returning to sight. In fact, I feel that there really is no concept of an “application” in Android - in the sense of something that has one entry and exit point.

+5
source share
4 answers

in Scenario 2, your activity will receive an onNewIntent call with which the intent starts.

+2
source

You can capture the "Back" button by clicking on "Activity B" and transfer the additional value to "Activity A." If there is an additional value, the action resumes after clicking on the action "B", if there is no additional value, then the action was not hidden.

+2

Acitive A startActivityForResult() Activity B onActivityResult() B?

+1

, , , : . () / .

, , :

  1. @superfell :

, onNewIntent onNewIntent -method , , . SingleTask/singleTop :

android:launchMode="singleTask"

, , ! , , , , , , . - . , , . onNewIntent , . , :

override fun onBackPressed() {
    moveTaskToBack(true)
}

, "" , , onNewIntent . startActivityForResult onActivityResult , , 2- startActivityForResult onActivityResult onActivityResult.

  1. Intent Extra

, "", "" . , , . onResumes, "":

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

"launcher":

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

All Articles