Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Fragments: Lifecycle method *always* called when fragment is displayed?

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,

Neil.

like image 448
Neil Avatar asked Nov 24 '12 17:11

Neil


People also ask

Which method is called once the fragment gets visible?

onStart()The onStart() method is called once the fragment gets visible. onResume()Fragment becomes active.

What fragment method is called when the fragment is no longer visible to the user?

onStop() fragment is no longer visible to the user either because its activity is being stopped or a fragment operation is modifying it in the activity. onDestroyView() allows the fragment to clean up resources associated with its View. onDestroy() called to do final cleanup of the fragment's state.

What are fragments explain the lifecycle methods of fragments?

Android Fragment is the part of activity, it is also known as sub-activity. There can be more than one fragment in an activity. Fragments represent multiple screen inside one activity. Android fragment lifecycle is affected by activity lifecycle because fragments are included in activity.

What method is automatically invoked when a fragment is instantiated?

Fragment and View STARTED When the fragment becomes STARTED , the onStart() callback is invoked.


1 Answers

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) {
    getSupportFragmentManager()
            .beginTransaction()
            .add(android.R.id.content, newFragment)
            .addToBackStack(tag)
            .commit();
}

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.

like image 166
Neil Avatar answered Oct 05 '22 17:10

Neil