导航抽屉onNavigationDrawerItemSelected在MainActivity onCreate之前调用?

时间:2014-05-21 08:40:42

标签: java android android-intent android-fragments navigation-drawer

我已经使用Navigation Drawer Fragment和MainActivity的模板实现设置了一个新项目。

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

@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获取保存的访问令牌。

这是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中选择的项目,它会启动三个不同的片段。在实例化新片段时,token字符串将传递到其构造函数中,该构造函数将保存在片段的类中以供进一步使用。

然而,在应用程序的第一次启动时,似乎onNavigationDrawerItemSelected之前调用了onCreate!这导致我将一个空值标记传递给片段,导致它们全部混乱。

这怎么可能?据我了解,NavigationDrawerFragment不应该已经设置好了!

我在onCreateonNavigationDrawerItemSelected switch position = 0上都设置了断点。在onNavigationDrawerItemSelected之前确实发现了onCreate

如何在尝试处理onNavigationDrawerItemSelected之前确保获取令牌?

任何帮助都将不胜感激。

3 个答案:

答案 0 :(得分:10)

我相信我发现了这一点,因为任何搜索此内容并且无法找到答案的人都会发现这一点。

如果您使用Android Studio DrawerActivity,那么他们会为您创建样板代码。在activity_main.xml中的此代码中,或者您的DrawerActivity设置为其内容视图的任何XML,都有一个标记。

当在onCreate()中调用setContentView()时,会自动创建此片段,因此技术上仍会首先调用onCreate(),然后在create之前调用onNavigationDrawerItemSelected()方法。由于setContentView通常保持最高,因此在尝试将片段状态存储在抽屉中时会出现问题。

只需移动任何检查上面setContentView()上面的savedInstanceBundle的代码,它就可以解决问题。

评论示例:

@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));

}

答案 1 :(得分:1)

您可以将意图移动到构造函数并将令牌保存在那里:

Intent i;

......

public FragmentConstructor() {

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

}

答案 2 :(得分:0)

我需要做的就是在执行onNavigationDrawerItemSelected之前检查页面是否已加载

    private Boolean loaded=false;

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

            // Your code here
            this.loaded=true;
    }

    public void onNavigationDrawerItemSelected(int position) {
        if (!this.loaded){
            return;
    }