문제

I'm developing an android app. I have an actionbar in tab navigation mode, and three tabs that call fragments to be replaced in the frameview container.

My problem is: if the user generates a backstack while in one tab and then switches to another tab, if the back button is pressed in this state- then the backstack is popped, but the tab doesn't change, which leads to overlapping fragments (backstack fragment which belongs to Tab1 is popped/added, but Tab2 is still selected and so is its fragment).

Is there a way to make actionbar tabs react to the back button in a way that will parallel the fragment backstack?

도움이 되었습니까?

해결책

I solved it by adding the tabs to the backstack with names (addBackStack("MainTab");) which I chose in the TabListener, and then overridden the onBackPressed() method in the activity to recognize when a tab is going to change when the backstack is popped (by getting the top BackStackEntry's name) and used actionBar.setSelectedNavigationItem(position) accordingly.

다른 팁

Instead of using addToBackStack(), override the onBackPressed() method in your activity to customize how you want the back key to behave.

This works for me :)

@Override
public void onBackPressed() {
    if (viewPager.getCurrentItem() == 0) {
        super.onBackPressed();
    }else {
        viewPager.setCurrentItem(0);
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top