定向更改后Android失去参考

时间:2016-08-16 08:51:31

标签: android android-layout screen-orientation

我有一个包含我的片段的MainActivity,并且我已经实现了一个主要活动实现它的接口MainView。我还创建了一个抽象的基本片段,我的所有片段都从它扩展而来。我在basefragment中声明了MainView。当我添加片段时,我正在设置基础片段的MainView onAttach。

@Override
    public void onAttach(Context context) {
        super.onAttach(context);
        try {
            this.mainView = (MainView) context;
        } catch (ClassCastException e) {
            throw new ClassCastException(context.getClass() + " must implement MainView");
        }
    }

然后我从像

这样的片段调用MainViews方法
public void showProgress() {
        if (mainView != null)
            mainView.showProgress();
    }

一切正常,但是当我旋转设备时,我再次设置主视图onAttach但是当我调用showProgress时,我看到mainView为null。我有点困惑为什么我得到mainView null。而且我在基础片段中声明SwipeRefreshLayout,并且在轮换后我再次拉动布局,progess显示但swipeRefreshLayout.isRefreshing()为假。所以我无法隐藏swipeRefreshLayout进展。

0 个答案:

没有答案
相关问题