Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing a Fragment with itself does not show anything

I'm trying to decide and show a fragment in activity's onResume method, but in case a previously added fragment is chosen again, then the activity goes blank.

Sample code (with one fragment):

@Override
protected void onResume(){
    FragmentTransaction trans = getSupportFragmentManager().beginTransaction();

    trans.replace(R.id.myLayout, fragA);

    trans.commit();
    getSupportFragmentManager().executePendingTransactions();
}

With above code, when the activity is created for the first time, it shows fragA correctly, but in case I press Home Key and then switch back to my activity (in order to provoke onResume again), it all goes blank (seems like fragA is removed).

Is replacing a previously added fragment removes itself? or how not to loose a fragment if it is replaced by itself?

like image 697
waqaslam Avatar asked Feb 19 '13 13:02

waqaslam


People also ask

How do I replace a fragment in a fragment?

Use replace() to replace an existing fragment in a container with an instance of a new fragment class that you provide. Calling replace() is equivalent to calling remove() with a fragment in a container and adding a new fragment to that same container. transaction. commit();

Can a fragment have its own view?

Fragments cannot live on their own--they must be hosted by an activity or another fragment. The fragment's view hierarchy becomes part of, or attaches to, the host's view hierarchy. Note: Some Android Jetpack libraries, such as Navigation, BottomNavigationView , and ViewPager2 , are designed to work with fragments.

Can fragment exist independently?

Fragment is dependent on activity. It can't exist independently. We can't create multi-screen UI without using fragment in an activity, After using multiple fragments in a single activity, we can create a multi-screen UI.

How do you check if a fragment is loaded or not?

You should be using FragmentManager 's findFragmentById() method, then you can check which fragment it is by using instanceof . Your code should look something like this: mFragmentManager = getSupportFragmentManager(); Fragment frag = mFragmentManager. findFragmentById(R.


2 Answers

You can't replace a fragment with itself. The first half of a replace is a removal of the previous fragment at that id. Once a fragment is removed it can no longer be added or used by the fragment manager (so the add portion of the replace will not work properly).

Depending on your use case, you have two options:

  1. Create a new fragment instead of reusing the existing instance
  2. Use some other method to see if its necessary to replace your fragment

Finally, you probably don't need to call executePendingTransactions.

like image 105
Justin Breitfeller Avatar answered Nov 15 '22 07:11

Justin Breitfeller


You can try:

if( !(getSupportFragmentManager().findFragmentById(R.id.myLayout) instanceof FragmentA) ) {
    FragmentTransaction trans = getSupportFragmentManager().beginTransaction();

    trans.replace(R.id.myLayout, fragA);

    trans.commit();
}

And I assume that fragA is FragmentA class object.

like image 40
Michał Z. Avatar answered Nov 15 '22 08:11

Michał Z.