1

Here is my main activity. I followed this guide about Fragments correctly. When I click "Back" button, my application is closed instead of returning to the MainScreenFragment. Why is this happening and why addToBackStack() doesn't work?

public class MainScreenActivity extends ActionBarActivity implements MainScreenFragment.OnFrameChoiced {


private MainScreenFragment mainScreenFragment;
private AddWordsFragment addWordsFragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_screen);

    mainScreenFragment = new MainScreenFragment();
    addWordsFragment = new AddWordsFragment();

    FragmentManager fm = getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    ft.add(R.id.container, mainScreenFragment).addToBackStack(null).commit();
}
@Override
public void choiceFrame(int id) {
    switch (id) {
        case R.id.add_new_words_frame:
            FragmentManager fm = getFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            ft.replace(R.id.container, addWordsFragment).addToBackStack(null).commit();
            fm.executePendingTransactions();
            break;
    }
}

P.S. I tried to use a solution from this topic, but It still doesn't work.

Community
  • 1
  • 1
gchubarich
  • 37
  • 1
  • 5

2 Answers2

1

did you try overriding the back like below:

@overide        
public void onBackPressed(){
            FragmentManager fm = getSupportFragmentManager();
                if (fm.getBackStackEntryCount() > 1) {
                    fm.popBackStack();
                } else {
                finish();
               }
        }
haha666413
  • 26
  • 4
  • Yeah, I did but It works only without if statement, just like this: `@Override public void onBackPressed() { getFragmentManager().popBackStack(); }` – gchubarich Mar 10 '15 at 10:44
0

(I know you have picked up your desirable answer, but I have found a little more against this problem)

Though as Android official site has documented:

By calling addToBackStack(), the replace transaction is saved to the back stack so the user can reverse the transaction and bring back the previous fragment by pressing the Backbutton.

But as a matter of fact, this is in a precondition that you are using the standard android activity, specifically, the android.app.Activity. Because this methods in android.app.Activity will work when Backbutton is pressed:

public void onBackPressed() {
    if (mActionBar != null && mActionBar.collapseActionView()) {
        return;
    }

    if (!mFragments.getFragmentManager().popBackStackImmediate()) {
        finishAfterTransition();
    }
}

But, if you are extending your xxxxActivity from someone else, for example, the AppCompatActivity, FragmentActivity, ActionBarActivity, it will be another story, because in FragmentActivity, onBackPressed() method is totally overrode:

public void onBackPressed() {
    if (!mFragments.getSupportFragmentManager().popBackStackImmediate()) {
        supportFinishAfterTransition();
    }
}

Note that mFragments.getFragmentManager() is replaced by mFragments.getSupportFragmentManager(), so in cases like this, you should begin your FragmentTransaction using getSupportFragmentManager() of the Activity. and as a consequence of that, you don't have to override onBackPressed method in your Activity.

BTW, ActionBarActivity extends AppCompatActivity extends FragmentActivity,they all come from the support library, you know what I mean, remember to use getSupportFragmentManager() instead of getFragmentManager() when using support library in order to get the compatible behavior.

Guofeng Pan
  • 531
  • 4
  • 5