Update actionbar dropdown properly

2019-09-15 10:15发布

问题:

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.

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;
}

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;
}

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

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.

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:

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

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

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

Fourth back button press: App exits

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

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

In Activity B:

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

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.

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?

Thanks in advance!

回答1:

Try using invalidateOptionsMenu() method

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

Description of invalidateOptionsMenu(): http://developer.android.com/reference/android/app/Activity.html#invalidateOptionsMenu()



回答2:

I finally figured it out! (And yes it was a simple solution, still lot's of learning to do in Android).

So in my original post I always set actionBar.setSelectedNavigationItem(int) in the onCreate method of the activities. However, when pressing the back button, the activity doesn't go through onCreate but it goes through onRestart(), onStart(), and onResume(), and that's why the dropdown list wasn't getting updated. In order to solve my problem, I simply call actionBar.setSelectedNavigationItem(int) inside of onResume() and now pressing the back button DOES update the dropdown :).