0

I created three tabs in my Main Activity using Fragment Activity, ActionBar, ViewPager. I have three tabs tab1, tab2, tab3. In tab1 two buttons are available, whenever user clicks on button i am replacing a fragment with another fragment using Fragment Manager and Fragment Transaction

Code to replace fragment :

   FragmentTransaction fragmentTransaction = fragmentManager
                            .beginTransaction();
                    CreditHistoryActivity creditHistoryFragment = new CreditHistoryActivity();

                    fragmentTransaction.replace(R.id.content,
                            creditHistoryFragment);
                    fragmentTransaction.addToBackStack("fragment1");
                    fragmentTransaction.commit();

Here, After pressing the button in tab2 the replaced content is overlapping on main content

Can u please tell me how to solve this issue.

Thank you.

  • Maybe this might help: http://stackoverflow.com/questions/22408600/why-doesnt-detach-work-on-fragmenttransaction-and-remove-does – nKn Mar 24 '14 at 10:37
  • why are you using "android.R.id.content" in the line fragmentTransaction.replace(android.R.id.content, creditHistoryFragment); – maddy d Mar 24 '14 at 11:25
  • I am adding fragment dynamically in my program. I did't mentioned fragment in my xml file – user3454856 Mar 24 '14 at 11:31

1 Answers1

0

If you fragments background color is transparent then all existing fragment show sometime. To remove fragments instance use below code in your fragment.

@Override
 public void onDetach() {
 super.onDetach();
}

 public void cleanupRetainInstanceFragment() {
  FragmentManager fm = getFragmentManager();
  fm.beginTransaction().remove(this.retainInstanceFragment).commit();
}  

In fragmentActivity do something like

  Fragment oldFragment = getSupportFragmentManager().findFragmentById(R.id.content);
    if (oldFragment instanceof CreditHistoryActivity) {
        ((CreditHistoryActivity)oldFragment).cleanupRetainInstanceFragment();
    }

    Fragment fragment = new CreditHistoryActivity();
    getSupportFragmentManager().beginTransaction().replace(R.id.content,fragment).commitAllowinStateLoss();

for details see this

Community
  • 1
  • 1
maddy d
  • 1,530
  • 2
  • 12
  • 23
  • Hi, thanks for your reply. I added onDetach() code in CreditHistoryActivity class and added below code in BalanceActivity class. I am trying to execute my application, again I am getting overlapping tab2 CreditHistoryActivity output is overlapping in tab3 content... – user3454856 Mar 24 '14 at 11:14