Is there a way to know if a activity's onresume is called because "back button" or if is called because the intire application Was in background ? I have to log this different actions :/ This is the scenario : I have 4 activity A-B-C-D ; i need to log the navigation between activity and i don't want to log the onresume caused by Application wake from background:)
3 Answers
From Activity A, when you will start the Activity B/C/D, use and identifier and start the activity for result.
For example:
int ActivityBID = 1;
Intent i = new Intent(this, ActivityB.class);
startActivityForResult(i, ActivityBID );
In your Activity B you override the onBackPressed
:
@Override
public void onBackPressed() {
Intent intent = new Intent();
intent.putExtra("activity","B")
setResult(RESULT_OK, intent);
finish();
}
And backing to Activity A:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == ActivityBID ) {
if(resultCode == RESULT_OK){
String stredittext=data.getStringExtra("activity");
}
}
}
Note: The inner if and the variable is not necessary, because you already know what activity you result are from, but, i think you may want to pass some data in this return, so i leave then.
Edit Image exemplify the ActivityForResult

- 735
- 4
- 9
-
In this way i only write the log if on back pressed event occurs ? For instance : A-B-C-B with this method I will write only B if the user close the app without return to the A ? – aeroxr1 Aug 15 '16 at 07:37
-
This way, you can log inside the `onActivityResult`, so the code will only be dispatched if the intent has an ActivityResult, when you close the APP, this don't occurs. You can create other variables like ActivityBID, to identify witch Activity the results are from. – Lucas Queiroz Ribeiro Aug 15 '16 at 12:39
-
But in your code is the setresult called only if the user press the back button ? – aeroxr1 Aug 15 '16 at 15:24
-
The only part who set the result is in the back, so yes. If you wanna, you can pass some identifier inside the set result and debug another situations – Lucas Queiroz Ribeiro Aug 15 '16 at 20:23
-
Then with this method I will not print anything in the following navigation : A-B-C ? – aeroxr1 Aug 16 '16 at 17:26
-
1If you start the Activity C using `startActivityforResult` from Activity B, the Activity B will have the result. I will post an Image with some explanation – Lucas Queiroz Ribeiro Aug 16 '16 at 18:32
-
@LucasQueirozRibeiroi have to try this method. But maybe the only problem is the first activity opened . I have to test ! – aeroxr1 Aug 17 '16 at 06:57
-
You APP start in several Activity's ? You can log the onCreate method to see which is started first. Or maybe you can always init on on activity and start all the other using start for result – Lucas Queiroz Ribeiro Aug 17 '16 at 13:28
When an Activity
is paused, onPause()
is called. In onPause()
write the name of the Activity
that is being paused into a global static
variable.
When an Activity
is resumed, onResume()
is called. In onResume
check if the global static
variable contains the name of this Activity
. If it does, you can omit the logs because this Activity
was just paused/resumed.

- 93,459
- 16
- 209
- 274
-
This way have a little problem. If the navigation is A-B-A-C , you will write only A-B-C . – aeroxr1 Aug 15 '16 at 07:30
-
1No. When returning to A from B, `B.onPause()` is called (will write "B" to the global), then `A.onResume()` is called (will see that the global is not "A", so it will log "A"). Have you actually tried it? – David Wasser Aug 15 '16 at 12:58
You can for instance override the onBackPressed()
callback
@Override
public void onBackPressed() {
super.onBackPressed();
/* add your log here */
}
Collecting the log from onResume()
plus the one (if any) coming from onBackPressed()
then you can detect the various scenarios.

- 5,084
- 3
- 21
- 39
-
I have 4 activity A-B-C-D ; i need to log the navigation between activity and i don't want to log the onresume caused by Application wake from background:) – aeroxr1 Aug 11 '16 at 17:37