I have downloaded https://www.androidhive.info/2017/12/android-working-with-bottom-navigation/ this code.After running the bottom tab is working fine,but when i am clicking on android back button that time the fragment is coming one step back,but the bottom navigation item is not coming as per fragment.
Suppose i have clicked "cart" and the fragment shows "My Cart" ,After this when i am clicking android back button ,the fragment is showing "My Gifts" but bottom navigation is still on "Cart".I want bottom tab should also come one step back.But I am not getting what is the problem here.Can anyone please help me out with this.
You can do one change. remove "transaction.addToBackStack(null);"
Why
FragmentTransaction addToBackStack (String name)
Add this transaction to the back stack. This means that the transaction will be remembered after it is committed, and will reverse its operation when later popped off the stack.
https://developer.android.com/reference/android/app/FragmentTransaction.html#addToBackStack(java.lang.String)
private void loadFragment(Fragment fragment) {
// load fragment
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_container, fragment);
/* Comment this line and it should work!*/
//transaction.addToBackStack(null);
transaction.commit();
}
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 upgraded my app from SDK 26 to SDK 28 and it ruined my app's navigation. I have a central tabbed activity. In each tab, the flow of the app is managed by fragments. I have implemented the back press in a way that the previous fragment of the current tab is shown. The back press code is:
public void onBackPressed() {
String name = fragmentManager.getBackStackEntryAt(
fragmentManager.getBackStackEntryCount() - 1).getName();
Fragment fragment = fragmentManager.findFragmentByTag(name);
String tag = ((AppFragment) fragment).getPreceddingFragmentTag();
Fragment frag = fragmentManager.findFragmentByTag(tag);
removeFragment(fragment);
replaceFragment(frag, tag);
}
I pass the current fragment's tag to the next in its constructor which is then fetched by the getPreceddingFragmentTag() method in the above code.
I use one method to show new fragments on the screen as well as remove them when back is pressed. Here's the code for that:
private void replaceFragment(Fragment fragment, String tag) {
loadedFragment = (AppFragment) fragment;
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(mContainerId, fragment, tag);
fragmentTransaction.addToBackStack(tag);
fragmentTransaction.commit();
invalidateOptionsMenu();
}
I maintain the backstack to keep track of transactions and get the currently displayed fragment when the user presses back.
The remove fragment method is:
private void removeFragment(Fragment fragment) {
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.remove(fragment);
fragmentTransaction.commit();
}
My app works in the following manner:
The tab opens up a fragment that contains a list that takes the user to the next fragment upon item click; which is also a list, tapping on an item in the list takes the user to the item's detail fragment.
Here's the issue:
I go from the first fragment to the next, then to the item details and come back. It works perfectly the first time.
I tap on another item on the list in the first fragment, it shows the next fragment perfectly, I tap on an item for details and then press back. Instead of showing the fragment that it was supposed to show, it shows the fragment that was shown in the previous flow.
I have ruined days and nights on stackoverflow, medium and Google Developers Documentation and have found no solution. My app was working perfectly with SDK 26 and 28 ruined everything. Can anyone help?
Same issue happened with me as well. In my case this issue happened due to following library:
androidx.appcompat:appcompat:1.1.0
Downgrade this library to androidx.appcompat:appcompat:1.0.0. This probably will fix your transaction issue. I am not sure if this is bug in library or they made any sort of improvement as i haven't found any particular notes in appcompat:1.1.0 release notes.
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);
I add, show and hide fragments. Each time I add/show a fragment, I hide the previous fragment and add the transaction to the backstack.
When a user presses the back button, a fragment is popped and I would like to have a reference to it.
Why do I need a reference? So I could hide it when the user continues to the next fragment.
So, how do I get a reference to a popped fragment?
EDIT-25-04-2013:
Here's code to explain how to add a new fragment, while hiding the previous one. The question is how to get a reference to the last fragment after it is popped from the backstack (using the back button)?
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.hide(lastFragment);
fragmentTransaction.add(newFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
lastFragment = newFragment;
I use generated tags for each fragment, save the tags in a stack and persist the stack. This way I get hold of every fragment out there - last one in specific.
See the code here.
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.