Skip to content Skip to sidebar Skip to footer

Android What To Use Instead Of Onrestart() In A Fragment

I'm dealing with .setVisibility() of a view, inside my main fragment at app start. So what I want is that the view is invisible on app start (for this i set INVISIBLE inside onCrea

Solution 1:

Fragments don't have onRestart(). It's only for Activities.

See the lifecycle of fragments below

enter image description here

I suppose you're looking for onResume() instead


Use a boolean flag to check whether or not you're returning to the Fragment:

privateboolean firstVisit;

@OverridepublicViewonCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    //other stuff
    firstVisit = true;
}

@OverridepublicvoidonResume() {
    //other stuffif (firstVisit) {
        //do stuff for first visit only

        firstVisit = false;
    }
}

Solution 2:

You need to use the onResume() callback method, if you would like to detect when the fragment is visible again

Solution 3:

You can use either onStart() or onResume() if you want to load things when returning to the fragment.

Solution 4:

Fragment life cycle doesn't have onRestart() method. You could use onPause() and onResume() as per your requirement.

Further reading : Fragments

Solution 5:

You can use onRestart() on the activity, making it call whatever method you want on the fragment by making use of getFragmentManager().findFragmentById(R.id.your_fragment). When a fragment gets restarted its underlying activity got restarted so its onRestart() method was called.

Post a Comment for "Android What To Use Instead Of Onrestart() In A Fragment"