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