Application has tabs
so that by selecting on every tab
I have the following code to update the fragment screen which exists below the tabs.
public class MainActivity extends ActionBarActivity{
Fragment f = null;
TabFragment tf = null;
List<Fragment> fragList = new ArrayList<Fragment>();
int tabIndex;
public void onTabSelected(Tab tab, FragmentTransaction ft) {
tabIndex = tab.getPosition();
if (fragList.size() > tab.getPosition())
fragList.get(tab.getPosition());
if (f == null) {
tf = new TabFragment();
Bundle data = new Bundle();
data.putInt("index", tabIndex);
data.putString("extraString", "noreloading");
tf.setArguments(data);
fragList.add(tf);
}
else
tf = (TabFragment) f;
ft.replace(android.R.id.content, tf);
}
So there is only one fragment class that I am updating with each tab.
Everything is working fine. I don't have any fragments in the backstack
. That is what I wanted. But I have a menu on actionbar
by selecting that option I should reload the fragment with new content. So I did the following in the mainactivity.
public void menuSelected(){
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
// fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE) added clear backstack here no use.
tf = new TabFragment();
Bundle data = new Bundle();
data.putInt("index", tabIndex);
data.putString("extraString", "reload");
tf.setArguments(data);
fragList.add(tf);
ft.addToBackStack(null);
ft.commit();
ft.replace(android.R.id.content, tf);
}
}
What happens here it reloads with new data, but the previous fragment exists in the backstack
when I press back button.
So I don't want any fragments in the backstack
, I just want to replace/reload the fragment content.
Can someone please suggest how to resolve this? Because suppose we have totally two fragments now one is the previous one and another is reloaded one. Now if we press another tab
and then menu again, now there are totally four fragments.