I have an activity that opens the browser. On the back press of the browser, the activity is invoked which needs to perform some operation. The problem is that onResume()
of the activity is invoked for the first time when it is created and also when the back arrow is pressed from the browser. I want to perform a specific operations on the back press of the browser. This logic should not be executed for the first time the activity is created. Is there any way to distinguish the flows or other ways to handle the back press from the browser?
Asked
Active
Viewed 41 times
0

MurugananthamS
- 2,395
- 4
- 20
- 49

Harini S
- 563
- 4
- 8
-
Add some flag as field to your activity, and set it to `true` in `onCreate`, and then check it and set to `false` in `onResume`. So when checking it, if it is `false`, then `onCreate` was not called, and you are returning to the app from browser. – Vladyslav Matviienko Oct 22 '19 at 04:34
-
`onRestart` is called when returning to the previous `Activity`. Use `onRestart` to distinguish. For more info visit https://developer.android.com/guide/components/activities/activity-lifecycle – Bek Oct 22 '19 at 04:38
-
you can try ```startActivityForResult``` and send back the result to your activity and perform your operation. follow this link: https://stackoverflow.com/a/10407371/8123836 – Sanju Rajpal Oct 22 '19 at 04:41
1 Answers
1
One way to do it is to make a Flag, say:
boolean isBrowserOpen = false;
public void openBrowser(){
startActivity(launchBrowserIntent);
isBrowserOpen = true;
}
public void onResume(){
if(isBrowserOpen){
isBrowserOpen = false;
// Your code here
}
}
It depends on what you really want to achieve
Edit:
To have more control of the browser's event, I suggest you should try to use in-app browser.

EricHo
- 183
- 10