Navigating to Previous Fragment Without Reloading It

Navigating to previous fragment without reloading it

you are replacing your fragments so the previous fragment will no longer be available with its content, you might try to load your fragments by "add" instead of "replace":

 transaction.add(R.id.content_data, newFragment);

How to navigate to previous fragment without reloading in navigation component architecture

I would suggest Dharmender's approach but if you are not using viewmodel then you can try this:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val tv1 = requireActivity().findViewById<TextView>(R.id.tv1)
tv1.setOnClickListener {
if(!isUpdated) {
isUpdated = true
requireActivity().supportFragmentManager.beginTransaction()
.replace((requireView().parent as ViewGroup).id, F2.newInstance())
.addToBackStack("f2").commit()
}else{
Toast.makeText(requireActivity(),"Already updated!", Toast.LENGTH_LONG).show()
}
}
}

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
Log.e(TAG,"On create view")
return inflater.inflate(R.layout.fragment_f1, container, false)
}

companion object {
@JvmStatic
fun newInstance() =
F1()
val TAG = F1::class.java.simpleName
var isUpdated = false
}

Here I am using one flag isUpdated.

Android Move to previous fragment without reload

Your issue is that you're recreating a new HomeFragment instance when you press back. You should instead pop the FragmentManager backstack.

 @Override
public void onBackPressed() {
if (fragment instanceof LoginFragment) {
LoginFragment loginFragment = (LoginFragment)fragment;
if(loginFragment.fromDrawer) {
getFragmentManager(mContext).popBackStack();
} else {
finish();
}
}
}

You also need to make sure you add the LoginFragment to the backstack.

public static void showLoginFragment(Context context,Boolean fromDrawer,String infoRegister)
{
final FragmentTransaction transaction =
getFragmentManager(context).beginTransaction();
transaction.replace(CONTAINER_ID,
LoginFragment.newInstance(fromDrawer,infoRegister),"login");
transaction.addToBackStack(null);
transaction.commit();
}

Handling Press Back Button without reloading fragment in Android

There are two ways to fix it.

  1. You can set the background for CatalogueFragment view, I mean background for ViewGroup when you call onCreateView inside CatalogueFragment

  2. Before call add Fragment you should get current Fragment by tag and make fragment transaction hide it.

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
List<Fragment> fragments = getSupportFragmentManager().getFragments();
if (!fragments.isEmpty()) {
for (Fragment fragment : fragments) {
if (!fragment.isHidden()) {
transaction.hide(fragment);
}
}
}

Android Fragment, going back without recreating/reloading Fragment

I believe that you are looking for show() and hide().

I think you can still add them to the backstack.

 transaction.hide(currentFragment);
transaction.show(detailScreen);
transaction.addToBackStack(null);
transaction.commit();

I didnt have my code to look at but i believe this is how it would go... Try it out unless someone else has a better way.
I have not tried the backstack with show() hide() but i believe that it takes the changes that are made before the transactions commit and will undo them if the back button is pressed. Please get back to me on this cause i am interested to know.

You also have to make sure that the detail fragment is created before you call this. Since it is based on the click of someitem then you should probably create the details fragment every time you click to make sure the correct details fragment is created.

How do you return to the previous fragment when using a NavController?

If all that you want to do is pop the back stack, setGraph() is unnecessary. Just call popBackStack() on your NavController.



Related Topics



Leave a reply



Submit