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