Android 完成 Intent 调用父级的 onCreate() [英] Android finishing Intent calls onCreate() of parent
问题描述
我遇到了我的应用程序由于 NullPointerException 而不断崩溃的问题.由于缺少值,无法进行数据库查询.
我已经调查过,以下情况正在发生:
类 A 是由 MainActivity 使用的(使用 IntentExtra int id
).Class A
在 B
上制作 Intent.B
通过单击返回"完成按钮(硬件后退按钮实际上有效.问题在于左上角的那个).A.onCreate()
再次被调用(但是创建它的 Intent 与以前不同,因此没有额外的 id).但我想回到 A
之前的状态.我需要一些变量实例.
I´ve encountered the problem that my app keeps crashing because of a NullPointerException. A databaseQuery cant be made because of a missing value.
I´ve investigated, that the following ist happening:
class A is intended by MainActivity (with IntentExtra int id
) .Class A
makes Intent on B
.
B
finishes by clicking the "back" button (the hardware back button works actually. The Problem is with that one in the upper left corner).
A.onCreate()
is called again (But the Intent creating this is not the same as before, thus not having the id Extra). But I´d like to return to the previous state of A
. There are instances of variables I need to have.
有什么解决办法吗?或者为什么会调用 onCreate ?
Is there any workaround? Or why is onCreate beeing called?
推荐答案
问题是:活动成员被抹杀"当停止.为了防止这种情况,您可以使用 SharedPreferences.
这里是一个很好的描述.-->保存持久状态
The Problem was:
Members of activitys get "wiped" when Stopped. To Prevent this you can use a SharedPreferences.
Here is a good discription.
--> Saving persitent state
这篇关于Android 完成 Intent 调用父级的 onCreate()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!