How to attach() and detach() instead of add() and remove() to save fragment view

五迷三道 提交于 2019-12-10 11:58:55

问题


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

回答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();
}


来源:https://stackoverflow.com/questions/17972345/how-to-attach-and-detach-instead-of-add-and-remove-to-save-fragment-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!