what method is called when Actionbar back/home button is pressed?

1.3k Views Asked by At

I have two activities first activity opens on app start and it holds fragment second activity is opened from that fragment, when I press home button second activity closes and in fragment methods onDetach -> onAttach -> onResume is called, but if I close second activity with finish() or onBackPressed() fragment is never detached just onResume() is called.So my question is what method is called when home button is pressed and is there possibility to use same method to close activity for example on button press? Basically what I need is that fragment would call onDetach -> onAttach when I closing second activity.

2

There are 2 best solutions below

0
On BEST ANSWER

you can remove the fragment in e.g. onDestroy() of the activity, so the fragment will go through it's lifecycle methods

1
On

The activities onPause() is called when pressing the home or back button. I just tried out using finish() and onBackPressed() which results in onPause() being called, too.

However, I am not sure whether there are differences when using fragments.

Android training concerning pausing and resuming: https://developer.android.com/training/basics/activity-lifecycle/pausing.html