重新使用/恢复片段而无需重新创建它

时间:2013-10-22 11:00:01

标签: android android-fragments android-activity

我基本上有一个分为两半的布局。在我的左边,我有按钮触发右侧布局中显示的各种片段。

enter image description here

当我点击每个按钮时,相应的片段被加载到片段显示区域中。某些片段(例如片段A和片段D)通过查询数据库或从互联网获取数据等来显示复杂数据。只要应用程序正在运行,此数据一旦加载就不会更改。我的问题是我可以将片段恢复到以前的状态并显示它。更清楚 - >我点击片段A按钮,在片段A类中完成并显示所有计算,然后我单击片段B并显示片段B然后显示C.现在当我单击片段A按钮时我只想要它加载回来数据,而不是重做计算/连接/数据库查询。

正在使用的代码:

   public void onClick(View v) {
   switch (position) {
            case 0:
                newContent = new FragmentA();
                break;
            case 1:
                newContent = new FragmentB();
                break;
            case 2:
                newContent = new FragmentC();
                break;
            case 3:
                    newContent = new FragmentD();
                break;
                case 4:
                newContent = new FragmentE();
                break;
                       default: break;
                  }
                     if (newContent != null){
         mContent = newContent;
         switchFragment(newContent);
        }

}

public void switchFragment(Fragment fragment) {
    mContent = fragment;
    getSupportFragmentManager()
    .beginTransaction()
    .replace(R.id.replacelayout, fragment)
    .commit();
}

片段代码示例

 public class FragmentA extends Fragment {

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragmenta, container, false);
    }   

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


   RUN ASYNTASKS TO CONNECT/QUERYDB AND DIPLAY THE DATA

}
    }

不知道如何去做 - 使用backStack? onResume()??因为我不确定在调用.replace时调用了什么函数。

先谢谢

2 个答案:

答案 0 :(得分:0)

您可以使用fragment.show和fragment.hide fragmentByTagName,而不是使用fragment.replace。这将使它们成为记忆中的状态。它可能看起来像这样。

   FragmentManager fragmentManager = getSupportFragmentManager();
       FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        //find the fragment by View or Tag
          MyFrag myFrag = (MyFrag)fragmentManager.findFragmentByTag(TAG);
          fragmentTransaction.hide(myOldFrag);
          fragmentTransaction.show(myNewFrag);
          fragmentTransaction.commit();

答案 1 :(得分:0)

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            Fragment newContent = null;
            getSupportFragmentManager()
            .beginTransaction()
            .hide(mContent)
            .commit();

            switch (position) {
            case 0:
                getSupportFragmentManager()
                .beginTransaction()
                .show(home)
                .commit();
                mContent = home;
                getSlidingMenu().showContent();
                break;
            case 1:

                if(ifdownexsists == true){
                     getSupportFragmentManager()
                        .beginTransaction()
                        .show(download)
                        .commit();
                        mContent = download;
                        getSlidingMenu().showContent();}
                else
                {   download = new DownloadFile();
                    getSupportFragmentManager()
                    .beginTransaction()
                    .add(R.id.replacelayout, download)
                    .commit();   
                    mContent = download;
                    ifdownexsists = true;
                    getSlidingMenu().showContent();
                }

                break;
            case 2:
                 if(ifexsists == true){
                     getSupportFragmentManager()
                        .beginTransaction()
                        .show(oldCheckValue)
                        .commit();
                        mContent = oldCheckValue;
                        getSlidingMenu().showContent();}
                else
                {   oldCheckValue = new CheckValues();
                    getSupportFragmentManager()
                    .beginTransaction()
                    .add(R.id.replacelayout, oldCheckValue)
                    .commit();   
                    mContent = oldCheckValue;
                    ifexsists = true;
                    getSlidingMenu().showContent();
                }
                break;

像魅力一样工作......甚至可以在后台保持进度条的进度,并在片段再次可见时正确显示它们