免责声明:我是一名经验丰富的程序员,但几乎是Android的新手;我可能会问这个显而易见的事。
我的暂定应用程序有一个相当简单的GUI。
具体来说,我有一个最顶级的Switch
来打开/关闭我的应用程序。
启动时,开关关闭。
当它打开时我可以从菜单中选择一个孩子MonitorActivity
来显示正在发生的事情(某种运行日志)。
MonitorActivity
与父母有适当的联系,所以我可以回到MainActivity
。
到现在为止还挺好。一切正常。
问题是当我反向导航MainActivity
完全重置,好像重新开始;特别是我的开/关Switch
回到off
位置。
我对relevant documentation的理解是MainActivity
应该已经停止;相关片段是:
之前的活动仍在堆栈中,但已停止。当活动停止时,系统将保留其用户界面的当前状态。当用户按下“返回”按钮时,当前活动将从堆栈顶部弹出(活动被销毁),之前的活动将恢复(其UI的先前状态将恢复)。
据我所知,我不应该做任何具体的恢复MainActivity
状态;它是否正确?
我应该发布我的AndroidMainfest.xml
(或其他文件)?
更新(根据要求):
我严格遵循MyFirstApp的模式:
MonitorActivity
从MainActivity
开始startActivity(intent)
。<activity android:name=".DisplayMessageActivity" android:parentActivityName=".MainActivity">
中的AndroidManifest.xml
)注意:这正是MyFirstApp
的行为:当从DisplayMessageActivity
回来时,EditText
小部件的内容消失了(即使没有明确地清除它)。
也许我应该重命名问题:“如何在'MyFirstApp'中'发送'之后保留'消息'?”
更新2:我压倒了几个回调来追踪真正发生的事情;这是日志(评论内联,寻找'<<<<<'):
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()
我究竟做错了什么?
您的问题与使用“向上导航”(即菜单栏中的箭头)相关,而不是使用“返回”按钮。我认为,如果你按下BACK按钮返回你的MainActivity
,一切都可以按你的需要工作。
您应该通过删除以下内容禁用“向上导航”:
android:parentActivityName=".MainActivity"
来自清单中的<activity>
声明。
当您了解如何自定义“向上导航”的行为时,您可以确保在使用它时返回时,不会创建先前Activity
的新实例,但会重用现有实例。
我认为您可以存储MainActivity的状态并使用startActivityForResult()启动子节点。
这是kotlin中的一个例子(我省略了代码的非相关部分):
class ProductDataActivity : BaseActivity(),ProductDataFragment.ProductDataListener {
private var productData: ProductBinding? = null
companion object {
private const val INSTANCE_STATE_PRODUCT_DATA = "state_product_data"
private const val RESULT_DATA = 1
}
override fun onSaveInstanceState(outState: Bundle?) {
outState?.putSerializable(INSTANCE_STATE_PRODUCT_DATA, this.productData)
super.onSaveInstanceState(outState)
}
override fun onRestoreInstanceState(savedInstanceState: Bundle?) {
this.productData = savedInstanceState?.getSerializable(INSTANCE_STATE_PRODUCT_DATA) as ProductBinding
super.onRestoreInstanceState(savedInstanceState)
}
override fun onDataClicked() {
val intent = TabProductDetailsActivity.getCallingIntent(this, productData!!, false)
this.startActivityForResult(intent, RESULT_DATA)
}
而其他活动:
class TabProductDetailsActivity : BaseActivity() {
private var productData: ProductBinding? = null //The activity is started with an intent as you can see above in the ProducDataActivity.
companion object {
const val INTENT_EXTRA_PRODUCT_DATA = "intent_product_data"
}
override fun onBackPressed() {
val resultIntent = Intent()
resultIntent.putExtra(INTENT_EXTRA_PRODUCT_DATA, productData)
setResult(Activity.RESULT_OK, resultIntent)
finish()
}
我希望你能理解我在这里做的事情,基本上我将一个对象从一个活动传递到另一个活动,当我按下后退按钮时,我没有启动活动的问题。不确定这究竟是你想要的,但我认为它可以为您提供许多方法之一。
事实证明,“罪魁祸首”是android.support.v7.app.AppCompatActivity
的使用,似乎(在经过大量调试之后)无条件地在requestDestroy()
发起的dispatchMessage()
结束时调用startActivity(intent)
。
onSaveInstanceState()
的默认实现尽职尽责地将小部件状态保存在Bundle
中,但这不会传递给MainActivity.onCreate(null)
。
制作public class MainActivity extends Activity { ...
(而不是public class MainActivity extends AppCompatActivity
)可以解决问题(它也会对ActionBar
和其他组件造成严重破坏,但这是另一个故事)。