0

I made an ActionBar with two tabs - one for afragment and bfragment. afragment contains a button. Clicking the button on afragment will result it to change to cfragment. However, clicking the tab again results to afragment being loaded again. What can I do to prevent this, to essentially say to the tab, "When somebody clicks on you, load the last fragment state (in this case, cfragment)?"

This is my MainActivity.java:

public class MainActivity extends Activity {

public static Context appContext;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    appContext = getApplicationContext();

    //ActionBar
     ActionBar actionbar = getActionBar();
     actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

     ActionBar.Tab PlayerTab = actionbar.newTab().setText("Fragment A");
     ActionBar.Tab StationsTab = actionbar.newTab().setText("Fragment B");

     Fragment PlayerFragment = new AFragment();
     Fragment StationsFragment = new BFragment();

     PlayerTab.setTabListener(new MyTabsListener(PlayerFragment));
     StationsTab.setTabListener(new MyTabsListener(StationsFragment));

     actionbar.addTab(PlayerTab);
     actionbar.addTab(StationsTab);
}

My TabListener:

class MyTabsListener implements ActionBar.TabListener {
public Fragment fragment;

public MyTabsListener(Fragment fragment) {
    this.fragment = fragment;
}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
    Toast.makeText(MainActivity.appContext, "Reselected!", Toast.LENGTH_LONG).show();
}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    ft.replace(R.id.fragment_container, fragment);
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    ft.remove(fragment);
}
}

And finally, my AFragment.java:

public class AFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.afragment, container, false);
    Button mButton = (Button) view.findViewById(R.id.button1);
    mButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            CFragment fragment = new CFragment();
            fragmentTransaction.replace(R.id.fragment_container, fragment);
            fragmentTransaction.commit();
        }
    });
    return view;
}
}

How do I save cfragment into the tab in such a way that clicking the first tab would not result to afragment loading again? Thank you.

Mnemone
  • 111
  • 1
  • 2
  • 9

1 Answers1

2

The state is not being saved because you are adding a new fragment every time you change tabs.

In 'onTabUnselected`, don't remove, but detach/hide

public void onTabUnselected(Tab tab, FragmentTransaction ft) {
  if (fragment != null) {
    ft.detach(fragment); 
  }
}

And in onTabSelected, check to see if the fragment exists already instead of replacing

public void onTabSelected(Tab tab, FragmentTransaction ft) {
  fragment = mActivity.getSupportFragmentManager().findFragmentByTag(tag);
  if( fragment == null ) {
    fragment = Fragment.instantiate(mActivity, mClass.getName());
    ft.add(R.id.fragment_container, fragment, tag);
  } else {
    ft.attach(fragment);
  }
}
loadedion
  • 2,217
  • 19
  • 41
  • the `onTabSelected` is giving me an error. Can you clarify it please? – Si8 Jul 30 '13 at 19:48
  • What's the error you're getting? Are you making sure that fragment is an instance variable? – loadedion Jul 30 '13 at 20:49
  • I have two tabs and one tab has some textboxes to enter data, once I switch to another tab and come back to the original tab the textboxes lose their values. That's what you are showing to fix correct? – Si8 Jul 30 '13 at 20:50
  • Yes, is it not saving, or is it simple crashing? – loadedion Jul 30 '13 at 22:57
  • I am just confused on the layout and the variables being used. I did try your method and I am not at my desk of the error I received but I can post my code and you can take look? – Si8 Jul 31 '13 at 00:31
  • Have you read the ActionBar implementation? http://developer.android.com/guide/topics/ui/actionbar.html#Tabs The example is pretty straightforward. It might be better to add the code to a new question (with the error code) – loadedion Jul 31 '13 at 16:34
  • If you can please take a look at my question: http://stackoverflow.com/questions/17972345/how-to-attach-and-detach-instead-of-add-and-remove-to-save-fragment-view – Si8 Jul 31 '13 at 16:36
  • `mFragment = mActivity.getSupportFragmentManager().findFragmentByTag(mFrag);` is giving an error: `The method getSupportFragmentManager() is undefined for the type Activity` – Si8 Jul 31 '13 at 17:18