Menma
Menma

Reputation: 809

remove current fragment, and replace with a new one

I tried to remove current fragment and replace with the first fragment PemegangPolis, everytime I click new button on my application, but I got nothing, this is my code :

case R.id.btn_new:
         android.app.FragmentTransaction fragTrans = getFragmentManager().beginTransaction();
         if (pemegangPolis.isAdded()) {
             fragTrans.remove(pemegangPolis);
             fragTrans.replace(R.id.frame_container, new PemegangPolis());
         }else if (tertanggungPolis.isAdded()) {
             getFragmentManager().beginTransaction().remove(tertanggungPolis);
             fragTrans.replace(R.id.frame_container, new PemegangPolis());
         }else if (usulanAsuransi.isAdded()) {
             getFragmentManager().beginTransaction().remove(usulanAsuransi).commit();
             fragTrans.replace(R.id.frame_container, new PemegangPolis());
         }else if (detailInvestasi.isAdded()) {
             getFragmentManager().beginTransaction().remove(detailInvestasi).commit();
             fragTrans.replace(R.id.frame_container, new PemegangPolis());
         }else if (detail_Agen.isAdded()) {
             getFragmentManager().beginTransaction().remove(detail_Agen).commit();
             fragTrans.replace(R.id.frame_container, new PemegangPolis());
         }else {
             if (dokumenPendukung.isAdded()) {
             getFragmentManager().beginTransaction().remove(dokumenPendukung).commit();
             fragTrans.replace(R.id.frame_container, new PemegangPolis());
         }
         }
         fragTrans.commit();
         mDrawerLayout.closeDrawer(SlidingMenu);
        break;

is there any wrong with my code? or am I missing something?

Upvotes: 1

Views: 3726

Answers (3)

Hamid Shatu
Hamid Shatu

Reputation: 9700

Try as follows...

FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

transaction.replace(R.id.fragment_container, new PemegangPolis());
transaction.addToBackStack(null);

transaction.commit();

Upvotes: 0

super-qua
super-qua

Reputation: 3188

Sometimes you are calling commit already on a transaction. This way the second (replace) is most probably not going to work.

Also, to perform a transaction, you don't have to remove the fragment at first. Just replace should be sufficient.

Depending on your app setup, you might need to replace getFragmentManager() with getSupportFragmentManager

case R.id.btn_new:
     android.app.FragmentTransaction fragTrans = getFragmentManager().beginTransaction();
     if (pemegangPolis.isAdded()) {

         fragTrans.replace(R.id.frame_container, new PemegangPolis());
     }else if (tertanggungPolis.isAdded()) {

         fragTrans.replace(R.id.frame_container, new PemegangPolis());
     }else if (usulanAsuransi.isAdded()) {

         fragTrans.replace(R.id.frame_container, new PemegangPolis());
     }else if (detailInvestasi.isAdded()) {

         fragTrans.replace(R.id.frame_container, new PemegangPolis());
     }else if (detail_Agen.isAdded()) {

         fragTrans.replace(R.id.frame_container, new PemegangPolis());
     }else {
         if (dokumenPendukung.isAdded()) {

         fragTrans.replace(R.id.frame_container, new PemegangPolis());
     }
     }
     fragTrans.commit();
     mDrawerLayout.closeDrawer(SlidingMenu);
    break; 

Upvotes: 0

mfrachet
mfrachet

Reputation: 8922

This code works for me :

Fragment fragment = new TypeOfFrag();

android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager(); fragmentManager.beginTransaction() .replace(R.id.frame_container, fragment).commit();

This code works for me. Try to adapt your code with this :-).

Upvotes: 3

Related Questions