I want to do a certain thing in my activity's fragment's onresume, but only if Back button has been pressed, and not if the app has been hidden behind another activity or "minimized" using "home" button/
How can I do that
I want to do a certain thing in my activity's fragment's onresume, but only if Back button has been pressed, and not if the app has been hidden behind another activity or "minimized" using "home" button/
How can I do that
onPause()
will be called when the Activity
is moved to the background. onPause()
is where you deal with the user leaving your activity. See the diagram here.
If another Activity
comes in onPause()
is called. You can override onPause()
. You can do anything/save anything there. When the activity again comes into foreground onResume()
is called. You can override that also, and restore anything there.
@Override
public void onPause() {
super.onPause(); // Always call the superclass method first
// Release the Camera because we don't need it when paused
// and other activities might need to use it.
if (mCamera != null) {
mCamera.release()
mCamera = null;
}
}
@Override
public void onResume() {
super.onResume(); // Always call the superclass method first
// Get the Camera instance as the activity achieves full user focus
if (mCamera == null) {
initializeCamera(); // Local method to handle camera init
}
}
For Back Button see this.
The onBackPressed
method is available from your Activity. Passing this to your Fragment could be done through a Broadcast, or possibly an Event bus (I'd recommend Otto for this). That way, your Activity can notify your Fragment of the onBackPressed
call.
You can do it easily: just use a variable in your sharedPreference and toggle it in onBackPressed in your activity to separate it from other actions that may cause onPause, then in onResume reset it :)