简体   繁体   中英

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 onCreateView) and to be visible when I come back to my fragment from other activities while the app is open (and for this I tried to use onRestart() to set view VISIBLE but it cannot resolve onRestart method) is onRestart deprecated or? thanks

EDIT: for all the answers below suggesting to use an onResume (and whom gave a -1), onResume doesn't work as onRestart at all, cause is being called right after onCreateView.

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;
    }
}

如果要在返回片段时加载内容,则可以使用onStart()onResume()

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.

如果您想检测片段何时再次可见,则需要使用onResume()回调方法。

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

Further reading : Fragments

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM