onPause/onRestore与savedInstanceState [英] onPause / onRestore with savedInstanceState

查看:88
本文介绍了onPause/onRestore与savedInstanceState的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对android开发非常陌生,我需要一些帮助来保存活动状态.从onPause保存实例并从onRestore还原实例的正确方法是什么,因为很明显,Android不会像在onCreate或onSaveInstanceState上那样发送saveInstanceState捆绑包.还是有比使用SavedInstanceState包更好的保存方式?

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?

这有意义吗?

好的,我想我知道我真正的问题是什么...但是首先,我想我正在寻找的是使用SharedPreferences而不是savedInstanceState.

[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.

因此,进行更多的调试日志观看时,我注意到,它不是在将Activity放在栈顶,而是在创建一个新的Activity.是的,我知道我正在创建一个新的....

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);

...但是FLAG_ACTIVITY_REORDER_TO_FRONT是否不应该阻止它创建新活动?我猜它认为自从我发送一些字符串以来,它必须创建一个新的?

...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?

更好的是,只要字符串相同,如何检查活动是否已在堆栈中并切换到该活动?-当用户从列表视图中单击媒体项目时,我将开始此活动.[/edit]

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]

推荐答案

由于某种原因,它没有以非常大胆的霓虹灯闪烁字母记录,因此我花了一段时间才发现,但您不必担心是否您的活动是否存在,如果您仅使用android:launchMode = [多个" |"singleTop" |"singleTask" |"singleInstance"]属性设置为"singleInstance".

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).

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

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">

然后,如果您仍然真的要保存要在应用程序将要关闭的情况下的值,则只需使用SharedPreferences.

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

这篇关于onPause/onRestore与savedInstanceState的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆