onPause / onRestore 与 savingInstanceState

发布于 2024-11-03 14:07:58 字数 928 浏览 1 评论 0原文

我对 Android 开发还很陌生,我需要一些帮助来保存活动的状态。 从 onPause 保存实例并从 onRestore 恢复实例的正确方法是什么,因为显然 Android 不会像 onCreate 或 onSaveInstanceState 那样发送 savingInstanceState Bundle。或者除了使用savedInstanceState包之外还有更好的保存方法吗?

这有道理吗?

[编辑] 好吧,我想我知道我真正的问题是什么......但首先,我认为我正在寻找的是使用 SharedPreferences 而不是 savingInstanceState。

因此,通过更多地观察调试日志,我注意到,它不是将 Activity 带到堆栈顶部,而是创建了一个新的 Activity。是的,我意识到我正在创建一个新活动......

         Intent itemintent = new Intent(MediaList.this, AudioPlayer.class);

         Bundle b = new Bundle();
        //...putString some strings to send
         itemintent.putExtra("android.intent.extra.INTENT", b);
         itemintent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
         startActivityForResult(itemintent,0);

但是 FLAG_ACTIVITY_REORDER_TO_FRONT 不应该阻止它创建新活动吗?我猜它认为它必须创建一个新的,因为我正在发送一些字符串?

更好的是,如何检查活动是否已经在堆栈中并在字符串相同的情况下切换到它? -- 当用户单击列表视图中的媒体项目时,我将开始此活动。 [/编辑]

I'm pretty new to android development and I need some help saving the state of an activity.
What is the correct way to save the instance from onPause and restoring it from onRestore since obviously Android isn't sending the savedInstanceState Bundle as it does with onCreate or onSaveInstanceState for example. Or is there a better way to save other than using the savedInstanceState bundle?

Does this make sense?

[edit]
Ok, i think i know what my real problem is... But first, I think what I was looking for was to use SharedPreferences instead of savedInstanceState.

So, doing more debug log watching I'm noticing that instead of bringing the Activity to the top of the stack it's creating a new one. Yes, I realize I'm creating a new one....

         Intent itemintent = new Intent(MediaList.this, AudioPlayer.class);

         Bundle b = new Bundle();
        //...putString some strings to send
         itemintent.putExtra("android.intent.extra.INTENT", b);
         itemintent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
         startActivityForResult(itemintent,0);

...But isn't FLAG_ACTIVITY_REORDER_TO_FRONT supposed to stop it from creating a new activity? I'm guessing it thinks it has to create a new one since i'm sending along some strings?

Better yet, how can I check if the activity is already in the stack and switch to it as long as the strings are the same? -- I'm starting this activity when the user clicks a media item from a listview.
[/edit]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

明媚殇 2024-11-10 14:07:58

由于某种原因,这并没有以非常非常大胆的霓虹灯闪烁字母记录下来,我花了一段时间才发现,但如果您只是使用
android:launchMode=["多个" | “单顶”|
“单一任务”| “单实例”]
属性设置为“singleInstance”。

那么它就只有一个实例,并且只要该活动没有被垃圾收集,您的内存字段就会被保留。

您可以做的另一件事是创建一个应用程序(从应用程序扩展)并将所有持久对象存储在其中...就您的整个应用程序的生命周期而言(包括您的所有活动),它首先创建,最后销毁- 较少的服务)。

只需创建一个 Application 类并在清单中指定它,如下所示:

<application android:icon="@drawable/icon" android:label="@string/app_name"
    android:name=".MyApplication">

然后,如果您仍然确实想保存应用程序将要关闭的情况下的值,只需使用 SharedPreferences 即可。

For some reason this is not documented in very very bold neon flashing letters, and took me a while to discover, but you don't need to worry about whether your activity exists or not if you simply create it with the
android:launchMode=["multiple" | "singleTop" |
"singleTask" | "singleInstance"]
property set to "singleInstance".

Then there is only ever one instance of it, and your memory fields are preserved as long as the activity hasn't been garbage collected.

The other thing you can do is to create an Application (extended from Application) and store all your persistent objects up in that... it is created first and destroyed last as far as your entire app's life cycle is concerned (including all your activity-less services).

Just create an Application class and specify it in your manifest like this:

<application android:icon="@drawable/icon" android:label="@string/app_name"
    android:name=".MyApplication">

Then if you still REALLY want to save your values for situations where the app is going to be closed, just use SharedPreferences.

不美如何 2024-11-10 14:07:58

你所说的 onRestore 方法是什么?它不是 Activity 生命周期的一部分...我想您的意思是 onRestart。无论如何,您没有获得 onRestart 捆绑包的原因是因为您不需要它。您的活动尚未被正式“终止”,因此您无需从保存的状态恢复。您的活动已暂停,但并未从内存中删除,因此系统只是告诉您它再次可见。您可能不需要为此类转换事件执行任何操作。

除此之外,方法是将您认为是“状态”值的任何内容保存在 onSaveInstanceState() 中,然后在 onCreate 中恢复它们。之后,您可以在 onCreate 本身或稍后在 Activity 生命周期(例如 onResume)中恢复任何特定于视图的属性。

What is this onRestore method you speak of? It is not part of the Activity lifecycle... I will suppose you mean onRestart. Anyway, the reason you don't get a bundle for onRestart is because you don't need one. Your activity hasn't been officially "killed" so you don't need to restore from a saved state. Your activity was paused, but not removed from memory, so the system is just telling you that it was made visible again. You probably don't need to do anything for this sort of transition event.

Other than that, the way to do it is to save anything you consider a "state" value in the onSaveInstanceState(), then restore them in onCreate. After that you can restore any view-specific properties either on the onCreate itself, or later in the Activity lifecycle (e.g. onResume).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文