P
P
Pavel2015-03-23 11:01:30
Android
Pavel, 2015-03-23 11:01:30

Why doesn't the hardware "back" button cancel the transaction?

Studying work with fragments, I came across the fact that the hardware button does not cancel the transaction.
In the Fragments|Performing Fragment Transactions section , the official documentation, it says that in order to cancel an action on a fragment, you need to add it to the backStack and commit the transaction. But, for some reason, my back button closes the current activity. Here is a code snippet where the content of the main container is replaced.

private void selectPage(int position) {
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction ft = fragmentManager.beginTransaction();
    switch (position) {
        case FRAGMENT_A_SHEET:
            ft.replace(R.id.fragment_content, new AFragment());
            break;
        case FRAGMENT_B_SHEET:
            ft.replace(R.id.fragment_content, new BFragment());
            break;
        case FRAGMENT_C_SHEET:
            ft.replace(R.id.fragment_content, new CFragment());
            break;
        case FRAGMENT_D_SHEET:
            ft.replace(R.id.fragment_content, new DFragment());
            break;
    }
    ft.addToBackStack(null);
    ft.commit();

    navigationDrawerLayout.closeDrawer(navigationDrawerList);
}

Or does the hardware back button need to be tracked separately?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
Copperfield, 2015-03-23
@PaulTMatik

Yes, you have to overload the onBackPressed() method and pop from the backStack yourself

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question