Activity 1 is visible. Press a button, and Activity 2 opens.
Activity 2 adds fragment A to itself (and back stack) and it displays fine
Pressing a button within the fragment transitions to another fragment, B
Press Back. Nothing happens. Huh? The Back press is seemingly absorbed and not acted upon, the display remains the same.
Press Back a second time, it reverts to the Activity 1, as expected.
Why is my fragment not being shown in step 4? I've added the fragment to the back stack, so why (when the Back button seems aware of its existence) does it not show the fragment?
Here's the code I'm using in Activity 2 to open Fragment A.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_profile_edit);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
transaction.addToBackStack(null);
transaction.add(android.R.id.content, new MyFragment());
transaction.commit();
}
And here's the code to open Fragment B
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
transaction.add(android.R.id.content, new MyOtherFragment());
transaction.commit();
Have you tried transaction.replace(...) instead of transaction.add(...)? That should work. I'm guessing because if you're just adding a fragment over another, it doesn't see transaction as wanting to go back fro Fragment A.
EDIT
The actual answer for the question is below in the comments: addToBackStack() should be used on the fragment which is replacing, not the one being replaced.
Related
I'm using the Android Studio template for Navigation Drawer, but I just can't figure it out how to properly navigate back from the new Fragments I added to the ones already existing.
I have seen many posts about similar problems, but couldn't fix it with the answers.
Fragment manager = getActivity().getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
And my many tentatives:
Gets back to an empty screen
transaction.replace(R.id.fl_nav_host_content_main, entitiesFragment);
transaction.addToBackStack(null);
Gets back to an empty screen
transaction.replace(R.id.fl_nav_host_content_main, entitiesFragment);
transaction.addToBackStack("GalleryFragment");
Gets back to an empty screen
transaction.replace(R.id.fl_nav_host_content_main, entitiesFragment, "GalleryFragment");
transaction.addToBackStack(null);
Gets back to an empty screen
transaction.replace(R.id.fl_nav_host_content_main, entitiesFragment, "GalleryFragment");
transaction.addToBackStack("GalleryFragment");
The new Fragment design shows incorrectly. At first does not navigate back; at second does navigate back twice
transaction.replace(android.R.id.content, entitiesFragment);
transaction.addToBackStack(null);
The new Fragment design shows incorrectly. At first does not navigate back; at second does navigate back twice
transaction.replace(android.R.id.content, entitiesFragment);
transaction.addToBackStack("GalleryFragment");
The new Fragment design shows incorrectly. At first does not navigate back; at second does navigate back twice
transaction.replace(android.R.id.content, entitiesFragment, "GalleryFragment");
transaction.addToBackStack(null);
The new Fragment design shows incorrectly. At first does not navigate back; at second does navigate back twice
transaction.replace(android.R.id.content, entitiesFragment, "GalleryFragment");
transaction.addToBackStack("GalleryFragment");
Can someone help?
Thanks in advance.
I'm struggling with a Fragment leak and I am not sure how to handle it:
I have a FragmentActivity(I'm using support lib v4) with two buttons: forth and back. Forth button adds a fragment to the backstack with a long animation and back button pops the fragment from the backstack.
Code for forth button click listener:
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.setCustomAnimations(R.anim.own_slide_in_left, R.anim.own_slide_out_right, R.anim.own_slide_in_left, R.anim.own_slide_out_right);
transaction.add(R.id.fragment_holder, new FirstFragment());
transaction.addToBackStack(null);
transaction.commit();
And for back button click listener:
getSupportFragmentManager().popBackStackImmediate();
When I press the back button and rotate the device while the animation is ongoing then after rotation visually everything seems to be ok, but the previous Fragment is leaked. I can tell it by having a breakpoint in, for example, Fragment's ctor or onSaveInstanceState(). It will be it on each rotation when it shouldn't be.
What am I doing wrong?
EDIT: This problem persists using native Fragments, Android 4.0.3
I have a form divided in few fragments. I call every fragment with:
final FragmentManager fm = getSupportFragmentManager();
final FragmentTransaction ft = fm.beginTransaction();
if(fragment.equals(this.formOne) || fragment.equals(this.formTwo)) {
ft.setCustomAnimations(android.R.anim.slide_in_left,android.R.anim.slide_out_right);
}
ft.replace(R.id.fragForm, fragment);
ft.addToBackStack(null);
ft.commit();
When I click on the back button it skips the previous Fragment and goes back to the Activity but with a blank screen.
For example, I have 3 fragments : A - B - C
If I go to C and want to go back to the previous Activity, I click on the back button so I'm on B, I click again and I'm on A, and when I click again, I have a blank screen, I need to click another time to come back to the previous activity.
I don't understand why have I this blank screen on my Activity.
I don't understand this in the developer documentation:
Note: You should not add transactions to the back stack when the
transaction is for horizontal navigation (such as when switching tabs)
or when modifying the content appearance (such as when adjusting
filters). For more information, about when Back navigation is
appropriate, see the Navigation design guide.
If we can't use that, what is the solution?
Don't add it to the backstack when you add fragment A.
ft.add(R.id.fragForm, fragmentA);
ft.commit();
ft.replace(R.id.fragForm, fragmentB);
ft.addToBackStack(null);
ft.commit();
ft.replace(R.id.fragForm, fragmentC);
ft.addToBackStack(null);
ft.commit();
If you don't have to Navigate to previous fragment don't add it to the backstack
Remove this
ft.addToBackStack(null);
In my application I have two fragments A and B. A fragment contains Google map and listview inside on it B fragment Google map only.
I want open B fragment from A fragment but when I press back button it has to go to A fragment without loading data.
public void addPage(final DefaultFragment pDefaultFragment, final boolean isAddToBackStack){
FragmentTransaction transaction = mFragmentManager.beginTransaction();
transaction.add(R.id.content_frame, pDefaultFragment);
//transaction.replace(R.id.content_frame, pDefaultFragment);
if (isAddToBackStack) transaction.addToBackStack(null);
transaction.commitAllowingStateLoss();
}
I use this code for adding fragment
Bframent b = new Bfragment();
addPage(b,true);
I know differences between transaction. add and transaction.replace. My problem is when I use transaction.add A fragment's map doesn't destroy it stays above of B fragment's map when pressing button. But when use transaction.replace after press back button data loads again.
So could anyone tell me what should I do to Press back button without to load data without any problem on view also.
On back press just provide your fragment null;
I have android screen layout shown below
Apps screen divided into 3 fragments, Header, Footer and Content. Header and Footer fragments are fixed. Content fragment is changing according to content. I replaces fragment1-fragment3 according to need. Initially Fragment1 is shows in content area. When i press a next button fragment1 is replace by fragment2. This is the order. I have question, if i press another button previous, how can i return to previous fragment ( fragment2 -> fragment1). Is any built in mechanism available in fragment class.
Please guide me...
thanks in advance
try this code
FragmentTransaction tx = fragmentManager.beginTransation();
tx.replace( R.id.fragment, new MyFragment() ).addToBackStack( "tag" ).commit();
Whenever you try to call new fragment then add back stack for it through
ft.addToBackStack(null);
And now if you want to back from one fragment to another then, make one method..
public void DeleteCurrentFragment()
{
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
Fragment currentFrag = getSupportFragmentManager().findFragmentById(R.id.detailFragment);
String fragName = "NONE";
if (currentFrag!=null)
fragName = currentFrag.getClass().getSimpleName();
if (currentFrag != null)
transaction.remove(currentFrag);
transaction.commit();
}
And after that call this:
getSupportFragmentManager().popBackStack();
DeleteCurrentFragment();
you can try something like:
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
ft.add(R.id.contentFragment, <your fragment>);
ft.addToBackStack(null);
ft.commit();
so suppose you're on fragment1 and after doing some stuff you want push fragment2 then on your contentFragment you can add or replace the fragment (fragment2 in this case) as per your requirement. Now when you call addToBackStack it means add this transaction to the back stack. So that the transaction will be remembered after it is committed, and will reverse its operation when later popped off the stack. Internally it maintains a stack so you don't want to do anything. At last when you press back button from fragment2 it checks whether the transaction having any fragment in its stack, if yes then it calls it. Its like top of stack. When you call addToBackStack in that stack fragment1 is added. So when you press back button stack's top is fragment1 so it calls it.
It happens when you press hardware back button. If you want to do this on any button, then on that button's click listener simply onBackPressed() method.