0

I have the following code for ActionBar:

private class MyTabListener implements ActionBar.TabListener
{
    private Fragment mFragment;
    private final Activity mActivity;
    private final String mFrag;

    public MyTabListener( Activity activity, String fragName )
    {
        mActivity = activity;
        mFrag = fragName;
    }

    @Override
    public void onTabReselected( Tab tab, FragmentTransaction ft )
    {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTabSelected( Tab tab, FragmentTransaction ft )
    {
        mFragment = Fragment.instantiate( mActivity, mFrag );
        ft.add( android.R.id.content, mFragment );
    }

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

I have some textboxes within those Tab fragments and switching between the tabs forces the app to lose any data I added to those textbox. Instead of add and remove, I would like to use attach and detach which saves the fragment state.

How do I accomplish that within the code that I already have?

Update:

The code now looks like this:

private class MyTabListener implements ActionBar.TabListener
{
    private Fragment mFragment;
    private final Activity mActivity;
    private final String mFrag;

    public MyTabListener( Activity activity, String fragName )
    {
        mActivity = activity;
        mFrag = fragName;
    }

    @Override
    public void onTabReselected( Tab tab, FragmentTransaction ft )
    {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTabSelected( Tab tab, FragmentTransaction ft )
    {
        //mFragment = Fragment.instantiate( mActivity, mFrag );
        //ft.add( android.R.id.content, mFragment );
          mFragment = mActivity.getSupportFragmentManager().findFragmentByTag(mFrag);
          if( mFragment == null ) {
            mFragment = Fragment.instantiate(mActivity, mClass.getName());
            ft.add(android.R.id.content, mFragment, mFrag);
          } else {
            ft.attach(mFragment);
          }
    }

    @Override
    public void onTabUnselected( Tab tab, FragmentTransaction ft )
    {
        //ft.remove( mFragment );
        //mFragment = null;
          if (mFragment != null) {
                ft.detach(mFragment); 
              }
    }
}

And I am getting an error for the following line:

mFragment = mActivity.getSupportFragmentManager().findFragmentByTag(mFrag);

Error:

The method getSupportFragmentManager() is undefined for the type Activity
Si8
  • 9,141
  • 22
  • 109
  • 221
  • 1
    The solution I would give here is the exact same as in http://stackoverflow.com/questions/16634247/save-fragment-state-inside-a-tab. What's the difference? – loadedion Jul 31 '13 at 16:42
  • I was just having a little trouble plugging in the variables between your code and my pre defined code :/ – Si8 Jul 31 '13 at 16:43
  • Did you get it working? I think the key component you're missing here is the tag when you do `ft.add(viewID,fragment,TAG)` – loadedion Jul 31 '13 at 16:46
  • Can I leave my code as is and add the TAG? And what would TAG be? – Si8 Jul 31 '13 at 16:56
  • In this case, the tag would be `mFrag`. When you initialize the TabListener, you send in `public MyTabListener( Activity activity, String fragName )` and set `mFrag = fragName`. You need to use this tag to retrieve the fragment. – loadedion Jul 31 '13 at 17:03
  • I updated my question with the updated code but I am receiving an error. Can you please check? Thanks – Si8 Jul 31 '13 at 17:11
  • Are you making sure to import the right libraries? the `supportFragmentManger` is from `android.support.v4.app.FragmentManager` – loadedion Jul 31 '13 at 17:19
  • Yes, I just added the IMPORT statement but it's still not working. – Si8 Jul 31 '13 at 17:24
  • check to see if your activity is extending `FragmentActivity` – loadedion Jul 31 '13 at 17:53
  • `public class MainActivity extends FragmentActivity {` and also: `import android.support.v4.app.FragmentManager;` If I change the line to: `mFragment = mActivity.getFragmentManager().findFragmentByTag(mFrag);` it doesn't give an error. – Si8 Jul 31 '13 at 17:57
  • IDK if the targetSDK has anything to do with it. I have the following: `` – Si8 Jul 31 '13 at 18:01

1 Answers1

1

Instead of rewriting your code, you could just use SharedPreferences to temporarily store the variables being displayed.

In the onDestroy method of the tab you're leaving, just add your variables to the preferences, and retrieve them when you re-enter the tab.

@Override
public void onDestroy(){
    super.onDestroy();
    Log.i("OnDestroy", "Logged");
    SharedPreferences prefs = getActivity().getSharedPreferences("name", Context.MODE_PRIVATE); 
    Editor editor = prefs.edit();
    editor.putString("stringName", stringToStore);
    editor.commit();
}
whitfin
  • 4,539
  • 6
  • 39
  • 67