Take the 2-minute tour ×

I need to execute some code whenever a fragment is displayed to the user.

Looking through the API the closest hook method I could see for this is onResume. However, from debugging through my code it seems that onResume is not called when the user selects the back-button to return to a previously displayed fragment.

Just to clarify the behaviour I am seeing:

I have a 'parent' activity which configures tabs in an ActionBar... and 2 fragments F1 and F2.

The user selects a tab and F1 is loaded into the activity. On a button press, the user can navigate to F2. This is not a separate tab... F2 is swapped into the layout.

Adding log statements, I can see that when the user selects the back-button on F2, the following happens....

1) onPause() is called on F2. 2) F1 is loaded into the activity... but onResume() on F1 is not called

There are no errors in the logs at this time.

Any ideas what I'm doing wrong here?

Thanks in advance,


share|improve this question
You should always post your relevant code so we can see what is happening. Do you add each fragment to the back stack? –  Sam Nov 24 '12 at 20:00
Thanks Sam. While editing my question and adding the code, I noticed my error was in my fragment switching-code. I've added an answer explaining. Cheers. –  Neil Nov 24 '12 at 21:01
add comment

2 Answers

up vote 4 down vote accepted

Ok, it seems this one was pilot-error. My original code to switch between fragments in an activity was....

public void switchFragment(Fragment newFragment, String tag) {
            .add(android.R.id.content, newFragment)

The call to add() in the above code should actually be to replace(). It seems that not replacing the fragment in the activity messes with the back-button behaviour.

Have to be honest and admit I'm not 100% sure exactly why this happens. However, after making the change, the onResume() method is called on the destination fragment, once the back-button is called.

share|improve this answer
so you downvoted my answer because you screwed up? How nice... –  Marcin Orlowski Nov 24 '12 at 23:17
I don't have the reputation required to downvote. I would actually have upvoted you, purely for the comedy value of misspelling 'RTFM'. –  Neil Nov 25 '12 at 0:02
Play nicely kids. –  Sandy Nov 13 '13 at 19:18
add comment

This bit odd question, because any existing class method is guaranteed to be called when the event that this method was designed to be called for occur and the conditions are met. Choose any that best fits task you want to accomplish. RTFM here

enter image description here

share|improve this answer
This is a bit of an odd answer as, in your rush to be smart, you obviously never read my question. And I did 'RTFM' (I assume that's what you meant) and the 'manual' did not help me. As I explained there is no life-cycle method I can see that is guaranteed to be called when a fragment is returned-to via a press of the back button. –  Neil Nov 24 '12 at 18:05
which element of diagram included in my answer is unclear? –  Marcin Orlowski Nov 24 '12 at 18:07
All of the diagram is clear, as it is part of the Android javadoc's - however, it does not answer my question. –  Neil Nov 24 '12 at 18:08
The side arrow is labeled "the fragment returns to the layout from the back stack". Choose any method after that point (best onResume()) and that's it. –  Marcin Orlowski Nov 24 '12 at 18:11
onPause() should be called on current fragment and onResume() on the one that you are going back to –  Marcin Orlowski Nov 24 '12 at 19:29
show 1 more comment

Your Answer


