Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/209.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 应用程序几个小时未使用后消失的片段_Android_Android Fragments_State - Fatal编程技术网

Android 应用程序几个小时未使用后消失的片段

Android 应用程序几个小时未使用后消失的片段,android,android-fragments,state,Android,Android Fragments,State,我正在使用打开左右菜单。这一切都很好,只是我找不到一种方法在应用程序至少几个小时(约6个小时)没有使用后保存片段的状态 以下是我在其中一个菜单中设置片段的方式: if (savedInstanceState != null) mContent = getSupportFragmentManager().getFragment( savedInstanceState, "mContent"); if (mContent == null) mContent = new

我正在使用打开左右菜单。这一切都很好,只是我找不到一种方法在应用程序至少几个小时(约6个小时)没有使用后保存片段的状态

以下是我在其中一个菜单中设置片段的方式:

if (savedInstanceState != null)
    mContent = getSupportFragmentManager().getFragment(
        savedInstanceState, "mContent");
if (mContent == null)
    mContent = new SomeFragment();

setContentView(R.layout.content_frame);
getSupportFragmentManager().beginTransaction()
    .replace(R.id.content_frame, mContent).commit();
当用户在较长时间后返回应用程序时,一些片段(左侧或右侧菜单中的片段)变为灰色,即使我知道,我尝试以下解决方案(左侧菜单的示例):

这没用


请注意,我也尝试了
commitAllowingStateLoss()
方法(而不是
commit()
),但也没有任何帮助。

我不确定,但我认为最好将状态数据保存在SharredPreferences或其他地方,然后手动重新创建。我认为保存实例状态更好地用于快速来回或旋转等。谁知道6小时内会发生什么事。Android可以清理内存或其他东西。你不能控制它。这是我的意见

我猜问题不在于片段,而在于其中的内容。您有菜单内容的适配器吗?检查它(可能会发布片段和相应视图/适配器的代码),似乎在重新展开片段后,它会显示相同的无效视图。

我认为您的问题与

根据那次尝试

ft.show(frag1)

应该是

ft.attach(frag1);等等,如果你遵循这个


有关详细信息

可能是系统终止了您的应用程序。它应该是全新启动,没有任何问题(savedInstanceState应该为空)。手动(从ddms或应用程序管理器)终止应用程序是否具有相同的效果?@kupsef我刚刚从应用程序管理器终止了它,并正确保存了实例状态。
    mLeftMenuFragment = new LeftMenuFragment();
    getSupportFragmentManager()
        .beginTransaction()
        .replace(R.id.sliding_menu_left_content_frame,
            mLeftMenuFragment).commit();