如何在选项卡中保持没有backstack的片段状态?

时间:2015-06-27 13:22:35

标签: android android-fragments tabs

我正在尝试在onSaveInstanceState中保存片段状态,但是当我回到片段时,它总是重新加载而不是从上一个状态开始。

我查看了onCreateViewonActivityCreated,并且onSaveInstanceState始终为空。

public void navigateFragment(String tag, Fragment fragment,
        boolean shouldAdd) {

    FragmentManager manager = getSupportFragmentManager();
    FragmentTransaction ft = manager.beginTransaction();


    if (shouldAdd)
        mStacks.get(tag).push(fragment); // push fragment on stack

    ft.replace(android.R.id.tabcontent, fragment);

    if (shouldAdd)
        ft.addToBackStack(tag);

    ft.commit();

    }

由于我无法使用backstack,因为在标签页中,堆栈无效。任何帮助都将受到高度赞赏。

7 个答案:

答案 0 :(得分:6)

在这种情况下,你必须管理片段'自己陈述。我不确切知道你的代码是如何工作的,所以我唯一能做的就是给你一些提示。

您需要实现的第一件事是保存片段的状态。让我们假设所有片段都有唯一的ID。在这种情况下,您需要创建一个将保留所有状态的地图:

private final Map<String, Fragment.SavedState> mFragmentStates = new HashMap<>();

private void saveFragmentState(String id, Fragment fragment) {
    Fragment.SavedState fragmentState = 
            getSupportFragmentManager().saveFragmentInstanceState(fragment);
    mFragmentStates.put(id, fragmentState);
}

您需要为要删除的片段调用此方法。然后我们需要恢复片段的状态以及我们如何做到这一点:

private void restoreFragmentState(String id, Fragment fragment) {
    Fragment.SavedState fragmentState = mFragmentStates.remove(id);
    if (fragmentState != null) {
        fragment.setInitialSavedState(savedState);
    }
}

在向事务添加片段之前需要调用此方法。

提供的代码应该可以正常工作,但要使其在活动娱乐上正常工作,我们需要正确保存和恢复mFragmentStates

private static final String KEY_FRAGMENT_STATES = "fragment_states";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    /* Your code ... */

    if (savedInstanceState != null) {
        Bundle fragmentStates =
                savedInstanceState.getParcelable(KEY_FRAGMENT_STATES);
        if (fragmentStates != null) {
            for (String id : fragmentStates.keySet()) {
                Fragment.SavedState fragmentState =
                        fragmentStates.getParcelable(id);
                mFragmentStates.put(id, fragmentState);
            }
        }
    }
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    /* Your code ... */

    Bundle fragmentStates = new Bundle(mFragmentStates.size());
    for (Map.Entry<String, Fragment.SavedState> entry : mFragmentStates.entrySet()) {
        fragmentStates.put(entry.getKey(), entry.getValue());
    }
    outState.putParcelable(KEY_FRAGMENT_STATES, fragmentStates);
}

您也可以查看FragmentStatePagerAdapter课程。它使用相同的方法来管理ViewPager的片段的状态。

更新:所以你的代码应该看起来像这样:

private Fragment mCurrentFragment;

public void navigateFragment(String tag, Fragment fragment,
        boolean shouldAdd) {
    FragmentManager manager = getSupportFragmentManager();
    FragmentTransaction transaction = manager.beginTransaction();

    if (shouldAdd) {
        mStacks.get(tag).push(fragment); // push fragment on stack
    }

    if (mCurrentFragment != null) {
        saveFragmentState(mCurrentFragment.getClass().getName(), mCurrentFragment);
    }

    mCurrentFragment = fragment;
    restoreFragmentState(fragment.getClass().getName(), fragment);
    transaction.replace(android.R.id.tabcontent, fragment);

    if (shouldAdd) {
        // You shouldn't use back-stack when managing fragment states by yourself.
        transaction.addToBackStack(tag);
    }

    transaction.commit();
}

在这个例子中,我使用片段的类名作为id,因此所有片段必须具有不同的类。但您可以使用任何其他唯一值作为ID。另外一件重要的事情是,在自己管理片段状态时,不应该使用反向堆栈。 Back-stack执行类似的状态管理,您可能会遇到冲突。

答案 1 :(得分:0)

我认为您应该使用共享首选项的概念,这比使用本地文件或数据库更快,并且更容易编码。一个很好的Android网页@ Storage Options。我会从网页上添加一些示例代码并更改一些以满足您的需求。

首先,在onCreate()覆盖方法中获取从首选项中保存的数据:

@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);

   SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
   boolean silent = settings.getBoolean("silentMode", false);
...
}

接下来让我们在片段停止或退出之前保存数据,原因有多种。通过覆盖onDetach方法实现此目的,网页覆盖onStop()代替。

@Override
public void onDetach() {
   super.onDetach();
   ...
   SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
   SharedPreferences.Editor editor = settings.edit();
   editor.putBoolean("silentMode", mSilentMode);

   // Commit the edits!
   editor.commit();
}
祝你好运,玩得开心!

答案 2 :(得分:0)

你正在做替换片段,这就是片段被破坏的原因。你可以尝试下面。在这里,我在单个 FragmentTransaction 中做了两件事,我添加一个新片段&amp; 隐藏现有片段。

假设我们在片段 A

上添加片段 B
    FragmentTransaction ft = fragmentManager.beginTransaction();
    ft.add(android.R.id.tabcontent, fragmentB, tagFragmentB)
            .hide(fragmentManager.findFragmentByTag(tagFragmentA))
            .addToBackStack(tag)
            .commit();

按下后,删除片段 B &amp; 显示具有相同状态的片段 A (在添加片段 B 之前)

请注意 tagFragmentA &amp; tagFragmentB 是片段 A &amp;分别添加 B

答案 3 :(得分:0)

我不清楚你用来实现Tabs的方式。 我猜,

ft.replace(android.R.id.tabcontent, fragment);

你可能已经实现了FragmentTabHost。

编写customFragmentTabhost并覆盖

 @Override
public void onTabChanged(String tabId) {
    FragmentTransaction t = getSupportFragmentManager().beginTransaction();
    if (tabId.equals(“Tab1”)) {
        TabFragment1 fragment1 = null;
        if (getSupportFragmentManager().findFragmentByTag(“Tab1”) == null) {
            fragment1 = new TabFragment1();
        } else {
            fragment1 = (TabFragment1) getSupportFragmentManager().findFragmentByTag("Tab1");
        }
        t.replace(R.id.realContent, fragment1, "Tab1").addToBackStack(null).commit();
    }
}

<强>更新

  1. 确保未在方向上重新创建活动,如果是这样,则设置为setRetainInstance(true),这样即使在方向更改时重新创建活动,也会保留碎片。

  2. 请为片段中的任何视图提供id。 Android系统维护状态非常重要。

答案 4 :(得分:0)

      clearBackStackEntry();
                        rl.setVisibility(View.GONE);

                        getSupportFragmentManager().beginTransaction()
                                .replace(FRAGMENT_CONTAINER, new HomeScreen())
                                .addToBackStack(null).commit();

     private void clearBackStackEntry() {
            int count = getSupportFragmentManager().getBackStackEntryCount();
            if (count > 0) {
                getSupportFragmentManager().popBackStack(null,
                        FragmentManager.POP_BACK_STACK_INCLUSIVE);
            }
        }



TRY THIS AND One more for fragment also try this: but use support.v4.app.Fragment, May be it will help you


Fragment fr = new main();
                android.support.v4.app.FragmentTransaction fragmentTransaction = getFragmentManager()
                        .beginTransaction();
                fragmentTransaction.replace(R.id.fragment_place, fr);
                fragmentTransaction.commit();
                // getActivity().finish();

答案 5 :(得分:0)

声明

setRetainInstance(true) 

在你的片段onCreate()中。重建以前的状态
链接有助于理解setRetainInstance的工作方式。

Understanding Fragment's setRetainInstance(boolean)

Why use Fragment#setRetainInstance(boolean)?

谢谢:)

答案 6 :(得分:0)

处理这些事情非常简单。我可以给你样品来处理我们添加的Fr代理的背压。

我已经声明了一个片段堆栈并将其中的所有片段推送到其中;

public static Stack<Fragment> fragmentStack;

制作一个这样的方法:

    public static void replaceFragementsClick(Fragment fragementObj,     Bundle bundleObj, String title){
        try {
            FragmentManager fragmentManager = ((FragmentActivity)     mContext).getSupportFragmentManager();
            if (fragementObj != null) {

            fragementObj.setArguments(bundleObj);
                fragmentManager.beginTransaction().replace(R.id.frame_container,     fragementObj).commit();
            } 

            DashBoardActivity.fragmentStack.push(fragementObj);


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

也尝试这个:

    public static void replaceFragementsClickBack(Fragment fragementObj, Bundle bundleObj, String title){
        try {
            FragmentManager fragmentManager = ((FragmentActivity) mContext).getSupportFragmentManager();
            if (fragementObj != null) {

            fragementObj.setArguments(bundleObj);
                fragmentManager.beginTransaction().replace(R.id.frame_container,     fragementObj).commit();

                DashBoardActivity.fragmentStack.pop();
            } 
        } catch (Exception e) {
            e.printStackTrace();
        }
}

在您添加的基本活动中,覆盖背景,如:

@Override
    public void onBackPressed() {
            /**
             * Do Current Fragment Pop
             * */           
            fragmentStack.pop();            

            if(fragmentStack.size() >0){

                Bundle bunldeObj = new Bundle();
                //******Exit from Current Fragment
               Fragment fragment = fragmentStack.pop(); 
//                  fragmentStack.push(fragment);
                    if(fragment instanceof PhotosFragment){
                    bunldeObj.putString("position", "4");               
                    replaceFragementsClick(fragment,bunldeObj,"Photos");
                }else if(fragment instanceof PhotoDetatilFragment){
                    bunldeObj.putString("position", "4");
                    replaceFragementsClick(fragment,bunldeObj,"Photos");
                }else if(fragment instanceof PhotoFullViewFragment){
                    bunldeObj.putString("position", "4");
                    replaceFragementsClick(fragment,bunldeObj,"Photos");
                }else if(fragment instanceof HomeFragment){
                    bunldeObj.putString("position", "4");
                    replaceFragementsClick(fragment,bunldeObj,"Home");
                }else if(fragment instanceof VideosFragment){
                    bunldeObj.putString("position", "4");
                    replaceFragementsClick(fragment,bunldeObj,"Videos");
                }else if(fragment instanceof VideoDetailFragment){
                    bunldeObj.putString("position", "4");
                    replaceFragementsClick(fragment,bunldeObj,"Videos");
                }else if(fragment instanceof VideoViewFragment){
                    bunldeObj.putString("position", "4");
                    replaceFragementsClick(fragment,bunldeObj,"Videos");
                }else if(fragment instanceof MusicFragment){
                    bunldeObj.putString("position", "4");
                    replaceFragementsClick(fragment,bunldeObj,"Music");
                }else if(fragment instanceof MusicListFragment){
                    bunldeObj.putString("position", "4");
                    replaceFragementsClick(fragment,bunldeObj,"Music");
                }else if(fragment instanceof InstallAppsFragment){
                    bunldeObj.putString("position", "4");
                    replaceFragementsClick(fragment,bunldeObj,"Apps");
                }else if(fragment instanceof MessageFragment){
                    bunldeObj.putString("position", "4");
                        replaceFragementsClick(fragment,bunldeObj,"Messages");
                }else if(fragment instanceof MessageDetailFragment){
                    bunldeObj.putString("position", "4");
                    replaceFragementsClick(fragment,bunldeObj,"Messages");
                }else if(fragment instanceof LocateDeviceFragment){
                    bunldeObj.putString("position", "4");
                    replaceFragementsClick(fragment,bunldeObj,"Locate     Device");
                }else if(fragment instanceof FilesFragmentBottomBar){
                    bunldeObj.putString("position", "4");
                    replaceFragementsClick(fragment,bunldeObj,"Files");
                }else if(fragment instanceof AppsFragment){
                    bunldeObj.putString("position", "4");
                    replaceFragementsClick(fragment,bunldeObj,"Apps");  



            }else {
                super.onBackPressed();

                Intent intent = new     Intent(DashBoardActivity.this,ConnectDeviceActivity.class);
                startActivity(intent);
                finish();
        }
    }