[英]Shared preferences - Bundles intent
I'am trying to put a shared preference in a bundle so i can use it in another class. 我试图将共享首选项放在一个包中,这样我就可以在另一个类中使用它。
So for example I have a class that views the strings inside a shared preference. 例如,我有一个类来查看共享首选项中的字符串。 Then i have another class which can edit the string.
然后我有另一个可以编辑字符串的类。
In my main class where i have created the bundle: 在我创建捆绑包的主类中:
SharedPreferences sharedpreferences;
Intent i = new Intent(getBaseContext(),verification.class);
i.putExtra("sharedpreferences", sharedpreferences);
The issue is with the putExtra. 问题在于putExtra。 it works for normal strings but not for the bundles, any ideas, i think its something simple
它适用于普通字符串但不适用于捆绑包,任何想法,我认为它很简单
Of course Intent.putExtra(...)
works with Bundles : 当然,
Intent.putExtra(...)
适用于Bundles :
Intent.putExtra(String name, Bundle value);
Anyways, there is no need to pass the SharedPreferences
as a Bundle
to the next Activity. 无论如何, 没有必要将
SharedPreferences
作为Bundle
传递给下一个Activity。 Simply retrieve the SharedPreferences
from the next Activity
itself. 只需从下一个
Activity
本身检索SharedPreferences
。
Save stuff into the SharedPreferences
: 将内容保存到
SharedPreferences
:
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(Context);
Editor e = sp.edit();
e.putString("key", "value"); // save "value" to the SharedPreferences
e.commit();
Retrieve stuff from the SharedPreferences
: 从
SharedPreferences
检索内容:
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(Context);
String s = sp.getString("key", null); // get "value" from the SharedPreferences
This makes not that much sense , but here is how to put a String
from the SharedPreferences
into an Intent
: 这没有多大意义 ,但这里是如何将一个
String
从SharedPreferences
放入一个Intent
:
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(Context);
Intent i = new Intent(Context, YourActivity.class);
i.putExtra("key", sp.getString("key", null));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.