0

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.

rick
  • 4,665
  • 10
  • 27
  • 44
  • Is that the actual code you used? What purpose does this piece of code would have `if (fragList.size() > tab.getPosition()) fragList.get(tab.getPosition());`? *by selecting that option I should reload the fragment with new content.* - which one? Should you update the current tab fragment? Why did you use the `ft.addToBackStack(null);` call if you don't want to have fragments in the backstack? – user Sep 03 '13 at 09:31
  • @Luksprog Is that wrong? Sorry, I used backstack(null) because as you can see I call the method menuSelected() when user selects an option so that I am recreating the fragment instead of reloading. So, I was trying to remove the backstack. But it doesn't work. I know I am completely wrong. Can you please tell me how to refresh a fragment and is this correct code that I am using with tabs? Actually I followed this tutorial http://www.survivingwithandroid.com/2013/06/android-action-bar-with-tab.html – rick Sep 04 '13 at 02:46
  • You didn't answered some of the questions above. Like, if the code you posted is the one you use? If you use the ActionBar tabs? If you use Actionbar tabs you may want to rethink your logic to only update the data in the fragment and not place a new Fragment covering the tab fragment which will not work very well. And don't use `addToBackStack(null)` because this adds the fragment transactions to the backstack. – user Sep 04 '13 at 06:45
  • @Luksprog I shifted my code to viewpager. Can you please look at this question? http://stackoverflow.com/questions/18609261/getting-the-current-fragment-instance-in-the-viewpager – rick Sep 04 '13 at 08:47

0 Answers0