更新动作条下拉正常 [英] Update actionbar dropdown properly

查看:117
本文介绍了更新动作条下拉正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用我有两个活动 - 活动A和活动B
活动A包含viewpager而活动B包含2个片段(同时添加)。这两个活动A和B有一个动作条下拉菜单,其中位置0 =去活动A和位置1 =去到活动B.

In my app I have two activities - Activity A and Activity B Activity A contains a viewpager while Activity B contains 2 fragments (added at the same time). Both Activity A and B have an actionbar dropdown menu where position 0 = go to Activity A and position 1 = go to Activity B.

在活动A的onNavigationItemSelected方法,我有:

In Activity A's onNavigationItemSelected method I have:

@Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
    switch (itemPosition) {
    case 0:
        return true;
    case 1:
        startActivity(new Intent(this, ActivityB.class));
        return true;
    }
    return false;
}

在活动B的onNavigationItemSelected我有:

In Activity B's onNavigationItemSelected I have:

@Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
    switch (itemPosition) {
    case 0:
        startActivity(new Intent(this, ActivityA.class));
        return true;
    case 1:
        return true;
    }

    return false;
}

在一个活动的onCreate我叫getActionBar()setSelectedNavigationItem(0);同样,在活动B的onCreate我叫getActionBar()setSelectedNavigationItem(1);

In Activity A's onCreate I call getActionBar().setSelectedNavigationItem(0); Similarly, in Activity B's onCreate I call getActionBar().setSelectedNavigationItem(1);

现在,每当我从下拉菜单中选择了一个项目,应用程序工作正常和下拉得到正确更新。然而,当我preSS回来,在下拉菜单中没有得到正确更新。事实上,它始终是相反的选择。

Now whenever I selected an item from the dropdown, the app works as expected and the dropdown gets updated correctly. However, when I press back, the dropdown menu does not get updated properly. In fact it is always on the opposite selection.

举例说,我打开应用程序,并通过动作条下拉从活动一展身手(这是第一屏)--->活动B ---->活动A ---->活动B所有。在这里,我preSS后退按钮:

For example say I open the app and go from Activity A (this is the first screen)--->Activity B---->Activity A---->Activity B all via the actionbar dropdown. From here I press the back button:

首先后退按钮preSS:我回去到活动A(但下拉选择仍然是Acitivty B)

First back button press: I go back to Activity A (but the dropdown selection remains as Acitivty B)

二后退按钮preSS:返回到活动B(下拉更新(不正确地)到活动A)

Second back button press: Go back to Activity B (dropdown updates (incorrectly) to Activity A)

第三后退按钮preSS:返回到活动A(下拉更新(不正确地)到活动B)

Third back button press: Go back to Activity A (dropdown updates (incorrectly) to Activity B)

第四后退按钮preSS:应用程序退出

Fourth back button press: App exits

我试图通过覆盖onBack pressed()在这两个活动来解决这个问题。
在活动答:

I tried to fix this by overriding onBackPressed() in both activities. In Activity A:

@Override
public void onBackPressed() {
    actionBar.setSelectedNavigationItem(1);
}

在活动B:

    @Override
public void onBackPressed() {
    actionBar.setSelectedNavigationItem(0);
}

这实际工作(下拉选择正确获取更新)。但是,应用程序不退出(但​​我希望它以后pressing回到上backstack最终退出活动),因为backstack是没有得到弹出,因为设置里面onBack pressed的selectedNavigationItem创建一个新意图到其他活动

This actually works (the dropdown selection gets updated correctly). However, the app never exits (but I want it to exit after pressing back on the final activity on the backstack), as the backstack isn't getting popped because setting the selectedNavigationItem inside onBackPressed creates a new intent to the other activity.

如何才能让这个pressing后退按钮弹出backstack(而不是创建一个新的intent),而正确更新导航下拉?我是什么,从这个看似简单的问题不见了?

How can make it so that pressing the back button pops the backstack (instead of creating a new intent) while correctly updating the navigation dropdown? What am I missing from this seemingly simple problem?

在此先感谢!

推荐答案

尝试使用invalidateOptionsMenu()方法

Try using invalidateOptionsMenu() method

@Override
public void onBackPressed() {
    getActivity().invalidateOptionsMenu();
}

invalidateOptionsMenu()的说明:
http://developer.android.com/reference/android/app/Activity.html#invalidateOptionsMenu()

这篇关于更新动作条下拉正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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