抽屉式导航栏MainActivity的onCreate前onNavigationDrawerItemSelected叫什么名字? [英] Navigation Drawer onNavigationDrawerItemSelected called before MainActivity onCreate?

查看:204
本文介绍了抽屉式导航栏MainActivity的onCreate前onNavigationDrawerItemSelected叫什么名字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经安装的模板实现导航抽屉片段和MainActivity一个新的项目。

I have setup a new project with the template implementation of Navigation Drawer Fragment and a MainActivity.

这为我提供了以下相关的方法:

It provides me with the following relevant methods:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Intent intent = getIntent();
    token = intent.getStringExtra(EXTRA_TOKEN);

    mNavigationDrawerFragment = (NavigationDrawerFragment)
            getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
    mNavigationDrawerFragment.activityMain = this;

    mTitle = getTitle();

    // Set up the drawer.
    mNavigationDrawerFragment.setUp(
            R.id.navigation_drawer,
            (DrawerLayout) findViewById(R.id.drawer_layout));
}

我MainActivity被飞溅该活动通过EXTRA_TOKEN获取已保存的访问令牌启动。

My MainActivity is started by a splash activity which gets a saved access token via the EXTRA_TOKEN.

这是抽屉式导航项目的覆盖请在MainAcitivity监听器:

This is the override of the Navigation Drawer item select listener in the MainAcitivity:

@Override
public void onNavigationDrawerItemSelected(int position) {
    // update the main content by replacing fragments
    FragmentManager fragmentManager = getSupportFragmentManager();
    onSectionAttached(position + 1);

    switch(position) {
        case 0:
            fragmentManager.beginTransaction()
                    .replace(R.id.container, FeedFragment.newInstance(token, ""))
                    .commit();
            break;

        case 1:
            fragmentManager.beginTransaction()
                    .replace(R.id.container, PeopleFragment.newInstance("", ""))
                    .commit();
            break;

        case 2:
            if(qbloggedin) {
                fragmentManager.beginTransaction()
                        .replace(R.id.container, MessagesFragment.newInstance(token, ""))
                        .commit();
            }
            break;

        default:
            break;
    }
}

有启动,这取决于产品在NavDrawer选择三种不同的片段。在实例新片段中,标记字符串传递到它的构造,这是保存在片段的类继续使用。

It starts three different fragments depending on which item is selected in the NavDrawer. While instantiating the new fragments, the token string is passed into its constructor, which is saved in the fragment's class for further use.

在不过该应用程序的第一次启动,似乎 onNavigationDrawerItemSelected 之前的onCreate 叫!这导致了我传递一个空值令牌插入片段,使他们成为全乱了。

On the first start of the App however, it seems that onNavigationDrawerItemSelected is called before onCreate! This results me passing a null value token into the fragments, causing them to be all messed up.

这怎么可能?据我了解,在NavigationDrawerFragment不应该被设定呢!

How is this possible? As I understand it, the NavigationDrawerFragment should not have been setup yet!

我设置断点都的onCreate onNavigationDrawerItemSelected开关位置= 0 onNavigationDrawerItemSelected 的onCreate 确实击中。

I set breakpoints on both onCreate and on onNavigationDrawerItemSelected switch position = 0. onNavigationDrawerItemSelected is indeed hit before onCreate.

我怎样才能确保先试图处理之前得到令牌的 onNavigationDrawerItemSelected

How can I make sure to get the token first before trying to handle the onNavigationDrawerItemSelected?

任何帮助将是AP preciated。

Any help would be appreciated.

推荐答案

我相信我想通了这一点,因为它是发生在我身上的人谁搜索这一点,并不能找到答案。

I believe I figured this out as it was happening to me for anyone who searches this and can't find the answer.

如果您使用Android工作室DrawerActivity再有就是他们为你打造样板code。在activity_main.xml中或任何XML您DrawerActivity集作为其内容视图此code,有一个标签。

If you use the Android Studio DrawerActivity then there is boilerplate code that they create for you. In this code in the activity_main.xml or whichever XML your DrawerActivity sets as its' content view, there is a tag.

当的setContentView()被调用的onCreate(),这个片段会自动创建并因此在技术上的onCreate()仍然被称为第一,但随后的onNavigationDrawerItemSelected()方法是什么,之前调用其他的创建。由于的setContentView通常保持向上顶,试图将片段的状态存储在你的抽屉时,这会导致问题。

When setContentView() is called in onCreate(), this fragment is automatically created and so technically onCreate() is still being called first but then the onNavigationDrawerItemSelected() method is called before anything else in create. Since setContentView is typically kept up top, this causes problems when trying to store the state of the fragments in your drawer.

只需将任何code以上的setContentView(检查savedInstanceBundle),它会解决这个问题。

Simply move any code that checks for savedInstanceBundle above setContentView() and it will fix the problem.

与注释示例:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // THIS IS WHERE YOU CHECK FOR SAVED INSTANCE
    // Check for frag
    if (savedInstanceState != null) {
        Log.i(TAG, "Get QuestionDayFragment");
        mQuestionDaysFragment = (QuestionDaysFragment) getSupportFragmentManager().getFragment(savedInstanceState, QUESTION_DAY_FRAGMENT);
    }

    // View injection
    setContentView(R.layout.activity_main);
    ButterKnife.inject(this);

    // THIS IS WHERE THE CODE WAS BEFORE
    // THIS WOULD BE CALLED AFTER onNavigationDrawerItemSelected()

    // Singleton injection
    LifeboxApplication.graph().inject(this);

    // Toolbar
    setSupportActionBar(mToolbar);

    // FB
    uiHelper = new UiLifecycleHelper(this, callback);
    uiHelper.onCreate(savedInstanceState);

    // Drawer
    mNavigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
    mTitle = getTitle();
    mNavigationDrawerFragment.setUp(R.id.navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout));

}

这篇关于抽屉式导航栏MainActivity的onCreate前onNavigationDrawerItemSelected叫什么名字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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