android在片段中使用什么代替onRestart() [英] android what to use instead of onRestart() in a fragment

查看:305
本文介绍了android在片段中使用什么代替onRestart()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理视图的.setVisibility(),它位于应用程序启动时的主要片段内. 所以我想要的是 view 在应用启动时不可见(为此,我在onCreateView内设置了INVISIBLE),并且当我在应用打开时从其他活动返回到我的片段时,该可见为此,我尝试使用onRestart()设置 view 可见,但无法解析onRestart方法) 是不赞成onRestart还是? 谢谢

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

编辑:对于以下所有建议使用onResume(并且给出-1的答案)的答案,onResume根本不像onRestart一样起作用,原因是在onCreateView之后立即调用. /p>

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.

推荐答案

片段没有onRestart().它仅用于活动.

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

请参见下面的片段的生命周期

See the lifecycle of fragments below

我想您正在寻找onResume()代替

使用布尔值标志检查您是否要返回片段:

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

这篇关于android在片段中使用什么代替onRestart()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆