1

I've implemented few Fragments in a FragmentTabHost, using:

Inside FragmentActivity Class:

FragmentTabHost mTabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.e(TAG, "onCreateView()");

    setContentView(R.layout.activity_main);

    mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
    mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

    mTabHost.addTab(mTabHost.newTabSpec("audio").setIndicator("Audio"),
            AudioFragment.class, null);
    mTabHost.addTab(mTabHost.newTabSpec("video").setIndicator("Video"),
            VideoFragment.class, null);

    mTabHost.setOnTabChangedListener(this);
}

In one of the fragment say AudioFragment there is a button which is supposed to open a new fragment over the current fragment of AudioFragment.

Inside AudioFragment.class

@Override
public void onClick(View button) {
    switch (button.getId()) {
    case R.id.bOpenNewFragment:

        // WHAT TO CODE, TO REPLACE THE CURRENT FRAGMENT OF AudioFragment 

        break;
    }

}

I tried the usual code using:

Fragment fragment = new ShowAudioDetailsFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.realtabcontent, fragment);
transaction.addToBackStack(null);
transaction.commit();

But, it was creating problems when I switched to another tabs (like state was not maintained & view of new fragment was merging with the layout of other tabs).

So How can I replace the fragment with the fragment opened within FragmentTabHost

Thank You

reiley
  • 3,759
  • 12
  • 58
  • 114

1 Answers1

0

Just add this line

((RelativeLayout) findViewById(R.id.realtabcontent)).removeAllViews(); // whatever layout you have

before :

transaction.replace(R.id.realtabcontent, fragment);
SweetWisher ツ
  • 7,296
  • 2
  • 30
  • 74
  • tried inside my `onclick()` inside `Fragment` class, not working: `((LinearLayout) getActivity().findViewById(R.id.realtabcontent)).removeAllViews();` – reiley Oct 22 '13 at 06:32
  • Sorry my bad, was getting class cast exception. Was using framelayout – reiley Oct 22 '13 at 06:49
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/39715/discussion-between-rani-and-raul8) – SweetWisher ツ Oct 22 '13 at 07:28