简体   繁体   中英

Android fragment state using navigation drawer

I'm creating an application that I am porting from windows phone 8 to android. My knowledge of android development are limited.

I'm using a navigation drawer. I've already done some implementation, the problems / issues I'm facing are restoring state of view when switching between them.

I'm using arcgis android sdk. View 1: geospatial data in a listview, fetched from the internet. View 2: a map showing your location and locations selected (tapped) in view 1 View 3: a settings view to increase or decrease the radius to fetch items from view 1.

When switching between views I'd like the views to maintain their states (restore state).

What's the best practice in a scenario like this? Right now I'm using fragments. Problem is that when switching fragments onSaveInstanceState is not being called.

This is my code to switch fragments, it's not working very well at all :)

private void selectItem(int position) {
    // Create a new fragment and specify the planet to show based on position
    Fragment f;
    FragmentTransaction ft = fragManager.beginTransaction();
    switch(position) {
    case 0: 
        if((f = fragManager.findFragmentByTag("home")) == null) {
            f = Fragment.instantiate(this, HomeFragment.class.getName());
            f.setRetainInstance(true);
            ft.add(R.id.content_frame, f, "home");
        } else {
            ft.attach(f);
        }
        break;
    case 1:
        if((f = fragManager.findFragmentByTag("map")) == null) {
            f = Fragment.instantiate(this, MapFragment.class.getName());
            f.setRetainInstance(true);
            ft.add(R.id.content_frame, f, "map");
        } else {
            ft.attach(f);
        }
        break;
    case 2:
        if((f = fragManager.findFragmentByTag("settings")) == null) {
            f = Fragment.instantiate(this, SettingsFragment.class.getName());
            f.setRetainInstance(true);
            ft.add(R.id.content_frame, f, "settings");
        } else {
            ft.attach(f);
        }
        break;
    }
    ft.commit();
    // Highlight the selected item, update the title, and close the drawer
    mDrawerList.setItemChecked(position, true);
    setTitle(tabs[position]);
    mDrawerLayout.closeDrawer(mDrawerList);

}

Here is the sample code for restore fragment state:

public void addFragments(Fragment fragment, 
        boolean addToBackStack, String tag) {

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

    if (addToBackStack) {
        ft.addToBackStack(tag);
    }
    ft.replace(R.id.content_frame, fragment);
    ft.commit();
}

Here fragment= your fragment that you want to replace.

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