【发布时间】:2019-02-27 11:43:05
【问题描述】:
免责声明:我是一名经验丰富的程序员,但几乎是 Android 新手;我可能会问显而易见的问题。
我的暂定应用程序有一个相当简单的 GUI。
具体来说,我有一个最上面的Switch 来打开/关闭我的应用程序。
启动时开关关闭。
当它打开时,我可以从菜单中选择一个子 MonitorActivity 来显示正在发生的事情(某种运行日志)。
MonitorActivity 已正确链接到父级,所以我可以返回到MainActivity。
到目前为止,一切都很好。一切正常。
问题是当我向后导航时MainActivity 完全重置,就像重新开始一样;特别是我的开/关Switch 回到off 位置。
我对@987654321@的理解是MainActivity应该已经停止了;相关的sn-p是:
前一个活动仍保留在堆栈中,但已停止。当一个 活动停止,系统保留其用户的当前状态 界面。当用户按下返回按钮时,当前活动 从堆栈顶部弹出(活动被破坏)和 上一个活动恢复(其 UI 的上一个状态是 已恢复)。
据我了解,我不应该做任何特定的事情来恢复MainActivity 状态;这是正确的吗?
我应该发布我的AndroidMainfest.xml(或其他文件)吗?
更新(根据要求):
我严格遵循MyFirstApp的模式:
- 使用
startActivity(intent)从MainActivity开始MonitorActivity。 - 使用主菜单上的左箭头进行后退导航(使用:
<activity android:name=".DisplayMessageActivity" android:parentActivityName=".MainActivity">inAndroidManifest.xml)
注意:这正是MyFirstApp 的行为:当从DisplayMessageActivity 回来时,EditText 小部件的内容消失了(即使没有明确清除它)。
也许我应该重命名问题:“如何在 'MyFirstApp' 中的 'Send' 之后保留 'message'?”
UPDATE2:我重写了几个回调来追踪真正发生的事情; 这是日志(cmets inline,查找'
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()
我做错了什么?
【问题讨论】:
-
我认为你使用了从 MonitorActivity 到 MainActivity 的意图,你能补充一下吗?
-
@GianlucaBenucci:谢谢。我按要求更新了问题。
标签: android android-activity navigation back