android what to use instead of onRestart() in a fragment

纵然是瞬间 提交于 2019-12-02 06:13:35

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

See the lifecycle of fragments below

I suppose you're looking for onResume() instead


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

private boolean firstVisit;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    //other stuff
    firstVisit = true;
}

@Override
public void onResume() {
    //other stuff
    if (firstVisit) {
        //do stuff for first visit only

        firstVisit = false;
    }
}
barq

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

fralmeida

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.

R4ng3LII

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

Tafveez Mehdi

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

Further reading : Fragments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!