Edit: Check if any activity (without knowing which) returned to desired activity
To check in Activity A, use:
@Override
protected void onResume() {
super.onResume();
// TODO: Work
}
As stated in the comment, onResume
will be called on an activity/fragment when:
- Activity runs for the first time
- Activity comes back into focus (from another activity, launcher, recent, another app)
However, you cannot track what triggered it, or what happened before it.
---------- Outdated ----------
Between Activity A and Activity B
use
startActivityForResult(intent, CHOOSE_AN_INT_VALUE_TO_INDICATE_IT_REQUESTS_FOR_BACK_PRESS);
In Activity A, and in Activity B, use
@Override
public void onBackPressed() {
setResult(CHOOSE_AN_INT_VALUE_TO_INDICATE_IT_CAME_FROM_BACK_PRESS);
finish();
}
Then again in Activity A, use
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CHOOSE_AN_INT_VALUE_TO_INDICATE_IT_REQUESTS_FOR_BACK_PRESS && resultCode==CHOOSE_AN_INT_VALUE_TO_INDICATE_IT_CAME_FROM_BACK_PRESS) {
// TODO: Do your work
}
}
If these 3 portions are implemented, you don't need to check for which activity triggered back press, you can simply compare the request and result codes
I hope this helps!!