I have a main activty which have a recyclerview from which i open a new fragment , lets say fragment1, now in the fragment 1 i have a scenario where i have to reload/repalce the same fragment , the issue with this method
FragmentTransaction transaction = requireActivity().getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, new make_everything_ok_btn());
transaction.addToBackStack(null)
transaction.commit();
is that if the scenario of the reload comes multiple times ,which im sure will come 100%, now this is adding multiple duplicates of the same fragmentent in the backstack
so for eg if i have to reload the fragment 3 times i have to press back 3 times to navigate back to the mainactivity screen
now a possible solution you can suggest is to remove the
transaction.addToBackStack(null);
which will not add the backstack, now in this scenario the main activity will not be navigated to, instead the app will be closed after 2 back press
so to come over this problem i had an idea what if instead of replacing the same fragment statically i just get the latest added fragment in the backstack for this eg its fragment1/make_everything_ok_btn() , and replace the same instance of the backstack with itself and now this time i will not add the addToBackstack , now what i understand from this idea is that this will replace the existing fragment with itself without adding a new same fragment to backstacl
i tried a code here it is
FragmentManager fragmentManager = requireActivity().getSupportFragmentManager();
int backStackEntryCount = fragmentManager.getBackStackEntryCount();
// Check if there are fragments in the back stack
if (backStackEntryCount > 0) {
// Get the name of the latest fragment in the back stack
String latestFragmentName = fragmentManager.getBackStackEntryAt(backStackEntryCount - 1).getName();
// Check if the current fragment is different from the latest fragment in the back stack
Fragment currentFragment = fragmentManager.findFragmentById(R.id.fragment_container);
if (currentFragment != null && !currentFragment.getClass().getSimpleName().equals(latestFragmentName)) {
// Get the latest fragment instance from the back stack
Fragment latestFragmentInstance = fragmentManager.findFragmentByTag(latestFragmentName);
// Replace the current fragment with a new instance of the latest fragment
if (latestFragmentInstance != null) {
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.fragment_container, latestFragmentInstance);
transaction.commit();
}else {
Log.d("backstack","latest fragment is null");
}
}else {
Log.d("backstack","Not same class");
}
}else {
Toast.makeText(getContext(), String.valueOf(backStackEntryCount),Toast.LENGTH_SHORT).show();
Log.d("backstack", String.valueOf(backStackEntryCount));
}
but this code is not working its not reloading the latest fragment, instead this log is printed Log.d("backstack","latest fragment is null");
which means there is no latest fragment available in the backstack available which is strange as i have navigated from mainactivity to fragment1 and i currently see the fragment1
please could anyone help me out here
please dont ask why im reloading the fragment, its complicated to explain it here
also here is the code where the fragments are opened from the rcyclerview in main activity
private void openFragmentBasedOnPosition(int position) {
FragmentTransaction transaction = fragmentManager.beginTransaction();
Fragment fragment = null;
String fragmentTag = null;
switch (position) {
case 0:
// Open Fragment associated with the first item
fragment = new make_everything_ok_btn();
fragmentTag = "make_everything_ok_btn";
break;
case 1:
// Open Fragment associated with the second item
fragment = new esc_ratrace_btn();
fragmentTag = "esc_ratrace_btn";
break;
case 2:
// Open Fragment associated with the second item
fragment = new delete_btn();
fragmentTag = "delete_btn";
break;
case 3:
// Open Fragment associated with the second item
fragment = new alttablife_btn();
fragmentTag = "alttablife_btn";
break;
case 4:
// Open Fragment associated with the second item
fragment = new givemespace_btn();
fragmentTag = "givemespace_btn";
break;
default:
break;
}
if (fragment != null) {
transaction.replace(R.id.fragment_container, fragment, fragmentTag);
transaction.addToBackStack(null);
transaction.commit();
}
}
Edit 1:- I also tried this solution
Fragment frg;
frg = requireActivity().getSupportFragmentManager().findFragmentByTag("make_everything_ok_btn");
final FragmentTransaction ft = requireActivity().getSupportFragmentManager().beginTransaction();
ft.detach(frg);
ft.attach(frg);
ft.commit();
but it dosent seem to work as well as its not reloading the fragment or maybe i can say not getting refreshed according to this
Removing the latest fragment from the backstack with
requireActivity().getSupportFragmentManager().popBackStack();
and then using the orignal code
FragmentTransaction transaction = requireActivity().getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, new make_everything_ok_btn());
transaction.addToBackStack(null);
transaction.commit();
gives me the desired output.
The combined code looks like this:
requireActivity().getSupportFragmentManager().popBackStack();
FragmentTransaction transaction = requireActivity().getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, new make_everything_ok_btn());
transaction.addToBackStack(null);
transaction.commit();