简体   繁体   English

使用导航抽屉的Android片段状态

[英]Android fragment state using navigation drawer

I'm creating an application that I am porting from windows phone 8 to android. 我正在创建一个将从Windows Phone 8移植到android的应用程序。 My knowledge of android development are limited. 我对android开发的知识是有限的。

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. 我正在使用arcgis android sdk。 View 1: geospatial data in a listview, fetched from the internet. 视图1:从互联网获取的列表视图中的地理空间数据。 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. 视图2:显示视图和在视图1中选择(点按)的位置的地图。视图3:用于增加或减小半径以从视图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. 问题是,在切换片段时,onSaveInstanceState没有被调用。

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. 片段=您要替换的片段。

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

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