在OnCreateView()之外获取片段的视图-视图为空

问题描述 投票:0回答:1

在onCreateView内部,我可以通过这种方式实例化视图:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        // Inflate the layout for this fragment
        if(savedInstanceState == null) {
            v = inflater.inflate(R.layout.fragment_my_team, container, false);
            setUpRecyclerView(v);
        }

        return v;
    }

现在,如果在第一个活动中返回时启动第二个活动,则在此片段中,View为null,因为已经调用了onCreateView。我不知道该视图的实例。

有解决方案吗?

android android-fragments view fragment-oncreateview
1个回答
0
投票

摆脱if(savedInstanceState == null)验证,并在每次调用onCreateView时创建并返回视图。

© www.soinside.com 2019 - 2024. All rights reserved.