简体   繁体   English

如何在导航抽屉中保存片段状态?

[英]How to save state of fragment in navigation drawer?

I am using navigation drawer in my android app and when i am reselecting fragment it loads twice. 我在我的Android应用程序中使用导航抽屉,当我重新选择片段时,它将加载两次。
Here is my code 这是我的代码

    private void displayView(int position) {
    // update the main content by replacing fragments
    Fragment fragment = null;
    switch (position) {
    case 0:
        // if(fraghome!=0)
        // {
        // fraghome=0;
        fragment = new HomeFragment();
        // }
        break;
    case 1:
        fragment = new BlogsFragment();
        break;
    case 2:
        fragment = new NewsFragment();
        break;
    case 3:
        fragment = new TransferFragment();
        break;
    case 4:
        fragment = new FixturesFragment();
        break;
    // case 5:
    // fragment = new BestXIFragment();

    // break;
    case 5:
        fragment = new FeedFragment();
        break;
    case 6:
        fragment = new TwitterFragment();
        break;
    case 7:
        fragment = new FacebookFragment();
        break;
    case 8:
        fragment = new BookmarkFragment();
        break;
    default:
        break;
    }

    if (fragment != null) {

        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction ft = fragmentManager.beginTransaction();
        ft.replace(R.id.frame_container, fragment).commit();

        // update selected item and title, then close the drawer
        mDrawerList.setItemChecked(position, true);
        mDrawerList.setSelection(position);
        setTitle(navMenuTitles[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
    } else {
        // error in creating fragment
        Log.e("MainActivity", "Error in creating fragment");
    }
}

In each of fragment i am using async task and when i am selecting fragment the async task started again and again 在每个片段中,我都使用异步任务,当我选择片段时,异步任务一次又一次地启动
please help me 请帮我

Normally in fragment, fragmens are destroyed, if it is not the current top fragment or the fragment just right or left to the current top fragment. 通常,在碎片中,如果碎片碎片不是当前的顶部碎片,也不是当前顶部碎片的右或左碎片,则碎片将被销毁。 So the asynctask is getting called again and again. 因此,asynctask被一次又一次地调用。 I assume you are getting some data using the asynctask to show in the fragment. 我假设您正在使用asynctask获取一些数据以显示在片段中。 So try to make the data source variable static and check null for the data source variable before executing asynctask. 因此,在执行asynctask之前,请尝试使数据源变量为静态,并为数据源变量检查null。 if the data source is not null then show the data. 如果数据源不为null,则显示数据。 I have added some abstruct code for your understanding. 我添加了一些抽象代码供您理解。 For better understanding you can see this link. 为了更好地理解,您可以查看此链接。 Hope it helps. 希望能帮助到你。 Thanks..... 谢谢.....

In each fragment..... 在每个片段中.....

public class TabFragment extends Fragment{
    private static DataSource ds = null;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        /*
        .
        other view initializing work
        .
        */
        if(ds == null){
            //execute asynctask and set data to ds in asynctask
        } else {
            // set data to view.
        }
        return view;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM