1

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

在此处输入图像描述

当我单击每个按钮时,相应的片段将加载到片段显示区域中。一些片段,例如片段 A 和片段 D,通过查询数据库或从 Internet 获取数据等来显示复杂的数据。只要应用程序正在运行,这些数据一旦加载就不会改变。我的问题是我可以将片段恢复到以前的状态并显示它。为了更清楚 - >我点击片段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 时会调用什么函数。

提前致谢

4

2 回答 2

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();
于 2013-10-22T11:11:36.203 回答
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;

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

于 2013-10-23T08:20:44.127 回答