如何绕过Android中的Intent Extras? [英] How to bypass intent extras in Android?
问题描述
我想知道是否有办法将所有额外功能传递给新意图.
I want to know if there is a way to pass all the extras to the new intent.
有可能吗?这是个好主意吗?
Is it possible? And is it a good idea?
原因是我想将登录状态另存为json字符串,并在Activity之间传递它.我不想用SQLite或任何东西存储数据.我可以绕过所有新的意图吗?
The reason is that I want to save the login state as a json string, and pass it in between Activities. I don't want to store data with SQLite or anything. Can I bypass all the intent extras to the new intent ?
推荐答案
请考虑共享首选项:
// Declaration
public static String KEY = "SESSION";
public static void saveUserName(String username, Context context) {
Editor editor = context
.getSharedPreferences(KEY, Activity.MODE_PRIVATE).edit();
editor.putString("username", username);
editor.commit();
}
public static String getUserName(Context context) {
SharedPreferences savedSession = context.getSharedPreferences(KEY,
Activity.MODE_PRIVATE);
return savedSession.getString("username", "");
}
您可以将登录凭据存储为首选项,并在任何活动中都可以检索它们.
You can store the login credentials to preference and retrieve them as well in any of your activity.
SaveUsername("Your text to save", Your_activity.this);
并获取值
String mUserName = getUserName(YourActivity.this);
建议将所有首选项方法都放在utils类中,以保持代码井井有条.
It is recommended to have all your preference methods in your utils class to keep the code organized.
您可以在此处
这篇关于如何绕过Android中的Intent Extras?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!