首页 > 解决方案 > android:返回导航后恢复Activity状态需要什么?

问题描述

免责声明:我是一名经验丰富的程序员,但几乎是 Android 新手;我可能会问显而易见的问题。

我的暂定应用程序有一个相当简单的 GUI。

具体来说,我有一个最顶层Switch可以打开/关闭我的应用程序。

启动时开关处于关闭状态。

当它打开时,我可以从菜单中选择一个孩子MonitorActivity来显示正在发生的事情(某种运行日志)。

MonitorActivity已正确链接到父级,因此我可以返回到MainActivity.

到目前为止,一切都很好。一切正常。

问题是当我向后导航MainActivity完全重置时,就像重新开始一样;特别是我的开/关Switch回到了off位置。

我对相关文件的理解MainActivity应该已经停止了;相关片段是:

前一个活动保留在堆栈中,但已停止。当活动停止时,系统会保留其用户界面的当前状态。当用户按下 Back 按钮时,当前 Activity 从栈顶弹出(该 Activity 被销毁)并恢复前一个 Activity(其 UI 的先前状态被恢复)。

由此我明白我不应该做任何特定于恢复MainActivity状态的事情。这个对吗?

我应该发布我的AndroidMainfest.xml(或其他文件)吗?

更新(根据要求):

我严格遵循MyFirstApp的模式:

注意:这正是以下行为:当从小部件的内容MyFirstApp回来时消失了(即使没有明确清除它)。DisplayMessageActivityEditText

也许我应该重命名问题:“我怎样才能在'MyFirstApp'中的'Send'之后保留'message'?”

UPDATE2:我覆盖了几个回调来追踪真正发生的事情;这是日志(内联评论,查找'<<<<<'):

02/27 16:05:24: Launching app
$ adb install-multiple -r -t -p it.condarelli.myfirstapp /home/mcon/AndroidStudioProjects/MyfirstApp/app/build/intermediates/split-apk/debug/slices/slice_2.apk 
Split APKs installed in 483 ms
$ adb shell am start -n "it.condarelli.myfirstapp/it.condarelli.myfirstapp.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -D
Waiting for application to come online: it.condarelli.myfirstapp | it.condarelli.myfirstapp.test
Waiting for application to come online: it.condarelli.myfirstapp | it.condarelli.myfirstapp.test
Connecting to it.condarelli.myfirstapp
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
I/elli.myfirstap: Not late-enabling -Xcheck:jni (already on)
W/elli.myfirstap: Unexpected CPU variant for X86 using defaults: x86
W/ActivityThread: Application it.condarelli.myfirstapp is waiting for the debugger on port 8100...
I/System.out: Sending WAIT chunk
I/System.out: Debugger has connected
    waiting for debugger to settle...
Connected to the target VM, address: 'localhost:8603', transport: 'socket'
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/chatty: uid=10085(it.condarelli.myfirstapp) identical 2 lines
I/System.out: waiting for debugger to settle...
I/System.out: debugger has settled (1354)
W/elli.myfirstap: JIT profile information will not be recorded: profile file does not exits.
I/chatty: uid=10085(it.condarelli.myfirstapp) identical 10 lines
W/elli.myfirstap: JIT profile information will not be recorded: profile file does not exits.
I/InstantRun: starting instant run server: is main process
I/MainActivity: onCreate(null)             <<<<< This is App start
W/elli.myfirstap: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (light greylist, reflection)
    Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (light greylist, reflection)
I/MainActivity: onStart()
I/MainActivity: onResume()
I/MainActivity: onPostResume()
D/OpenGLRenderer: HWUI GL Pipeline
D/: HostConnection::get() New Host Connection established 0xcb81c0c0, tid 17161
I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
    android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasHDRDisplay retrieved: 0
I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Swap behavior 1
W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
D/OpenGLRenderer: Swap behavior 0
D/EGL_emulation: eglCreateContext: 0xde3584a0: maj 2 min 0 rcv 2
D/EGL_emulation: eglMakeCurrent: 0xde3584a0: ver 2 0 (tinfo 0xcb943060)
D/EGL_emulation: eglMakeCurrent: 0xde3584a0: ver 2 0 (tinfo 0xcb943060)
I/AssistStructure: Flattened final assist data: 2344 bytes, containing 1 windows, 8 views
I/MainActivity: onPause()                  <<<<< This is first message after I pressed 'Send' button.
W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@18e99e1
I/DisplayMessageActivity: onStart()
I/DisplayMessageActivity: onResume()
I/DisplayMessageActivity: onPostResume()
D/EGL_emulation: eglMakeCurrent: 0xde3584a0: ver 2 0 (tinfo 0xcb943060)
D/EGL_emulation: eglMakeCurrent: 0xde3584a0: ver 2 0 (tinfo 0xcb943060)
D/EGL_emulation: eglMakeCurrent: 0xde3584a0: ver 2 0 (tinfo 0xcb943060)
D/EGL_emulation: eglMakeCurrent: 0xde3584a0: ver 2 0 (tinfo 0xcb943060)
D/EGL_emulation: eglMakeCurrent: 0xde3584a0: ver 2 0 (tinfo 0xcb943060)
I/MainActivity: onStop()
I/MainActivity: onSaveInstanceState()      <<<<< This is where log stops while DisplayMessageActivity is focused
I/DisplayMessageActivity: onPause()        <<<<< This is first message after Back-navigation
I/MainActivity: onDestroy()                <<<<< WHY THIS NOW??
W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@bd33145
I/MainActivity: onCreate(null)             <<<<< MainActivity is recreated from scratch
I/MainActivity: onStart()
I/MainActivity: onResume()
I/MainActivity: onPostResume()
D/EGL_emulation: eglMakeCurrent: 0xde3584a0: ver 2 0 (tinfo 0xcb943060)
D/EGL_emulation: eglMakeCurrent: 0xde3584a0: ver 2 0 (tinfo 0xcb943060)
D/EGL_emulation: eglMakeCurrent: 0xde3584a0: ver 2 0 (tinfo 0xcb943060)
I/DisplayMessageActivity: onStop()
I/DisplayMessageActivity: onDestroy()

我究竟做错了什么?

标签: androidandroid-activitynavigationback

解决方案


您的问题与您使用“向上导航”(即:菜单栏中的箭头)而不是使用“返回”按钮有关。我假设,如果您按 BACK 按钮返回到您的MainActivity,一切都会按您的意愿进行。

您应该通过删除以下内容来禁用“向上导航”:

android:parentActivityName=".MainActivity"

<activity>清单中的声明。

当您详细了解如何自定义“向上导航”的行为时,您可以确保在使用它返回时,Activity不会创建先前的新实例,而是重用现有实例。


推荐阅读