1

I'm writing an android application using a FrameLayout and Fragments as its component.

I have 3 fragments: CommunityFragment, TaskFormFragment, TaskFragment.

In CommunityFragment, user presses a button to create a task. Then TaskFromFragment replaces with CommunityFragment in the FrameLayout with addToBackStack. Because after that, I want to return to CommunityFragment with back button.

In TaskFormFragment, user presses create button to create the task. Then TaskFragment opens without addToBackStack because I don't want to show TaskFormFragment when back button is presssed.

All of them working perfect. When I press back button in TaskFragment, CommunityFragment is shown. However, after that point, if I open TaskFormFragment and press back button, it doesn't show CommunityFragment! Instead, it opens TaskFragment.

How this can happen? Here is my fragment transition code:

From CommunityFragment to TaskFormFragment:

TaskFormFragment newFragment = new TaskFormFragment();
Bundle args = new Bundle();
args.putLong("taskTypeId", taskTypeId);
args.putLong("communityId", community.getId());
newFragment.setArguments(args);

FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.frame_container, newFragment);
transaction.addToBackStack(null);
transaction.commit();

From TaskFormFragment to TaskFragment:

TaskFragment newFragment = new TaskFragment();
Bundle args = new Bundle();
args.putLong("taskId", task.getId());
newFragment.setArguments(args);

FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.frame_container, newFragment);
transaction.commit();

EDIT: I found the solution in this entry: Problems with Android Fragment back stack

Community
  • 1
  • 1
sedran
  • 3,498
  • 3
  • 23
  • 39

2 Answers2

4

I've found the solution in another post. I've added that methods into my Activity class which contains the FrameLayout:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        if (getSupportFragmentManager().getBackStackEntryCount() == 0) {
            this.finish();
            return false;
        } else {
            getSupportFragmentManager().popBackStack();
            removeCurrentFragment();
            return false;
        }
    }

    return super.onKeyDown(keyCode, event);
}


public void removeCurrentFragment() {
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    Fragment currentFrag =  getSupportFragmentManager().findFragmentById(R.id.detailFragment);

    if (currentFrag != null)
        transaction.remove(currentFrag);

    transaction.commit();
}

The description and the solution of this problem is here: Problems with Android Fragment back stack

Community
  • 1
  • 1
sedran
  • 3,498
  • 3
  • 23
  • 39
1

Once the user presses back in Task fragment and it goes back to community fragment, you should clear your backstack. Your problem is that it is not cleared so when you press back you go to a previous transaction.

EDIT:

So before your community fragment is replaced run this

for(int i =0;i<fragmentmanager.getBackStackEntryCount();i++){
fragmentmanager.popbackstack();
}

See if that fixes it

Cam Connor
  • 1,231
  • 2
  • 25
  • 40
  • But I didn't add TaskFragment to backstack? So I didn't even give a name to it. How can I clear it? – sedran Jan 07 '14 at 23:11
  • the problem is that TaskFragment replaces with CommunityFragment. I'm not adding TaskFragment to backstack. It returns to TaskFragment from TaskFormFragment and it removes CommunityFragment from backstack. I cannot return to it. – sedran Jan 07 '14 at 23:29