简体   繁体   中英

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.
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. I assume you are getting some data using the asynctask to show in the fragment. So try to make the data source variable static and check null for the data source variable before executing asynctask. if the data source is not null then show the data. 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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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