通过按钮可以调用其他活动的共享首选项 [英] Shared preferences from other activity to be call with button

本文介绍了通过按钮可以调用其他活动的共享首选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个尝试理解sharedpreferences的初学者.当我的共享首选项程序运行时,一切进展顺利.

I'm a beginner trying to understand sharedpreferences. Everything is going smoothly as my program of shared preferences run as I want it to be .

我的输入在活动1中,使用共享的首选项,我在活动2中将其回叫.

My inputs are in activity 1 and using shared preferences, I call them back in activity 2.

但是如何仅通过使用活动2中的按钮,如何使用共享的首选项将活动1中的输入称为活动3 ?

推荐答案

sharedpreference存储到常量类,并使用静态变量而不是设置并从该类中获取值随时随地.

Store sharedpreference to Constant class and use static variables than set and get values from that class anytime you want.

设置首选项中的值:

MY_PREFS_NAME - a static String variable like: 

public static final String MY_PREFS_NAME = "MyPrefsFile";

SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
 editor.putString("name", "Amit");
 editor.putInt("idName", 888);
 editor.commit();

从偏好设置中检索数据:

SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); 

    String restoredText = prefs.getString("text", null);
    if (restoredText != null) {
      String name = prefs.getString("name", "No name defined");  //"No name defined" is the default value.
      int idName = prefs.getInt("idName", 0);    //0 is the default value.
    }

检查此答案以获取更多详细信息.

check this answer for more details.

这篇关于通过按钮可以调用其他活动的共享首选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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