导航时MainActivity调用onDestroy

时间:2013-12-16 21:20:55

标签: android android-fragments back

我的应用中的导航存在问题。我有两个活动,MainActivity和FormActivity。主要活动有一个抽屉布局,根据所选项目加载一个片段:

private void selectItem (int position) {
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager
            .beginTransaction();

    Fragment fragment = null;
    previousNavigationItem = position;

    switch (position)
    {
        case DASHBOARD_FRAGMENT:
            fragment = new DashboardFragment();
            mTitle = getResources().getString(R.string.title_dashboard);
            break;
        case LIST1_FRAGMENT:
            fragment = new ListFragment();
            mTitle = getResources().getString(R.string.title_list1);
            break;

        case LIST2_FRAGMENT:
            fragment = new ListFragment();
            mTitle = getResources().getString(R.string.title_list2);
            break;

        default:
            Log.w(AppController.ERROR_TAG, "Index out of bounds in changeFragment");
    }

    if (fragmentTransaction != null && fragment!=null) {
        fragment.setRetainInstance(true);
        fragmentTransaction.replace(R.id.content_frame, fragment);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
    }
}

ListFragment加载包含一些行的列表视图,当选择一个项目时,它会启动一个新的FormActivity:

protected void startForm(FormActivity.FORM_ACTION action, String dataClass, int groupPosition, long groupId) {
    Intent data = new Intent(getActivity(), FormActivity.class);
    data.setAction(action.toString());
    data.putExtra(FormActivity.BUNDLE_KEY_GROUP_POSITION, groupPosition);
    data.putExtra(FormActivity.BUNDLE_KEY_FORM_CLASS, dataClass);
    data.putExtra(FormActivity.BUNDLE_KEY_GROUP_ID, groupId);
    startActivityForResult(data, FormActivity.FORM_REQUEST_CODE);
}
在FormActivity中:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_form);

    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager
            .beginTransaction();

    Fragment fragment = null;
    try {
        (...I do some casting here but nothing to do with the problem..)
        fragment = new FormFragment();
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

    if (fragmentTransaction != null && fragment!=null)
        fragmentTransaction.replace(R.id.form_frame, fragment).commit();

}

一切都按预期工作,除非我使用HomeUp按钮导航回来,MainActivity被销毁然后再次重新创建,并且不保留savedInstance。

我已经阅读了Android开发者的所有文档,但我仍然没有找到罪魁祸首。

修改:

在MainActivity中

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    Log.d(AppController.DEBUG_TAG, "MainActivity onRestoreInstanceState");
    super.onRestoreInstanceState(savedInstanceState);
    // Restore the previously serialized current dropdown position.
    if (savedInstanceState.containsKey(STATE_SELECTED_NAVIGATION_ITEM)) {
        getSupportActionBar().setSelectedNavigationItem( savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM));
    }

}

@Override
public void onSaveInstanceState(Bundle outState) {
    Log.d(AppController.DEBUG_TAG, "MainActivity onSaveInstanceState");
    super.onSaveInstanceState(outState);
    // Serialize the current dropdown position.
    outState.putInt(STATE_SELECTED_NAVIGATION_ITEM,
            getSupportActionBar().getSelectedNavigationIndex());
    Log.d(AppController.DEBUG_TAG, "Selected item id in Navigation Drawer: " + mDrawerList.getSelectedItemId());
}

1 个答案:

答案 0 :(得分:1)

见:

http://developer.android.com/guide/topics/manifest/activity-element.html#lmode

基本上,在任何给定时间都可以进行多种同类活动。

还要了解onDestroy可以随时根据系统资源调用,最好为此做好准备。

编辑:

好的,我似乎对某些事情有所了解。

您将不得不使用SharedPreferences或其他一些持久性方法。如果android完全关闭了你的应用程序,那么你的savedInstance包的资源可能会被释放。

共享偏好是一种非常标准的持久效果,并且效果非常好......

请参阅:

http://developer.android.com/guide/topics/data/data-storage.html#pref