Android片段生命周期问题与操作栏 [英] Android fragment lifecycle issue with actionbar

查看:98
本文介绍了Android片段生命周期问题与操作栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用以下代码实现片段的导航:

public abstract class BaseFragment extends Fragment {
    private static String TAG = "BaseFragment";
    private BaseFragmentActions baseFragmentActions;
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        Activity activity = null;
        if (context instanceof Activity){
            activity = (Activity) context;
        }
        Log.i(TAG, "onAttach = ");
        try {
            baseFragmentActions = (BaseFragmentActions)activity;

        } catch (ClassCastException e) {
        }
        Log.i("onAttach",""+(getBackStackCount()!=0));
        baseFragmentActions.resetToolbarNavigation(getBackStackCount()!=0);
    }
    @Override
    public void onDetach() {
        super.onDetach();
        Log.i("BaseFragment", "onDestroy = " + (getBackStackCount() - 1));
        baseFragmentActions.resetToolbarNavigation((getBackStackCount() - 1) != 0);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    private int getBackStackCount() {
        int b = getActivity().getSupportFragmentManager().getBackStackEntryCount();
        Log.i("getBackStackEntryCount", "====== "+b);
        return b;
    }

    public interface BaseFragmentActions {
        public void resetToolbarNavigation(boolean backNavigationEnabled);
    }
}

我所有的片段都扩展了此基本活动.在我的主要活动中,我实现了BaseFragmentActions,并实现了以下方法:

 @Override
    public void resetToolbarNavigation(boolean backNavigationEnabled) {
        Log.i("BaseActivity", "reset " + backNavigationEnabled);
            getSupportActionBar().setDisplayHomeAsUpEnabled(backNavigationEnabled);
            if (backNavigationEnabled) {
                mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Log.i("resetToolbarNavigation", "setNavigationOnClickListener");
                        onBackPressed();
                    }
                });
            } else {
                initNavigation();
                syncState();
            }
    }

一切正常,但是当我更改屏幕方向时,我们会得到错误,即getSupportActionBar = null. 这是由于我所说的要附加的内容.如何解决此错误?我试图检查getSupportActionBar是否不为零.我没有收到错误消息,但向上"箭头取代了汉堡包...

Everything works fine but when I change the screen orientation we obtain error that getSupportActionBar = null. This is because of what I call going to attach. How can I fix this error? I tried to make checking whether getSupportActionBar is not zero. I'm not getting an error, but "up" Arrow replaced hamburger...

建议您在这种情况下可以做什么.还共享链接以导航此类片段的实现.抱歉,如果写错了,或者我犯了语法错误).

推荐答案

抱歉,回答延迟,您遇到的问题是因为尚未调用onAttach时设置了getSupportActionBar(),而是需要以确保与Activity组件进行交互时已经创建了Activity,因此只需将您的调用放在Fragment的onActivityCreated方法中即可,如下所示:

Hi sorry for the delay in the answer, the problem you're having is because when onAttach is called the getSupportActionBar() is not set yet, instead you need to make sure the Activity is already created when interacting with Activity components, so just put your call inside the onActivityCreated method of your Fragment like this:

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    baseFragmentActions.resetToolbarNavigation(getBackStackCount()!=0);
}

这篇关于Android片段生命周期问题与操作栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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