如何在应用程序的生命周期中创建全局变量,而不管哪个活动正在运行。


当前回答

你可以扩展基本的android.app.Application类,并像这样添加成员变量:

public class MyApplication extends Application {

    private String someVariable;

    public String getSomeVariable() {
        return someVariable;
    }

    public void setSomeVariable(String someVariable) {
        this.someVariable = someVariable;
    }
}

在你的android manifest中,你必须声明实现android.app. application的类(添加android:name="。属性到现有的应用程序标签):

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

然后在你的活动中,你可以像这样获取和设置变量:

// set
((MyApplication) this.getApplication()).setSomeVariable("foo");

// get
String s = ((MyApplication) this.getApplication()).getSomeVariable();

其他回答

Technically this does not answer the question, but I would recommend using the Room database instead of any global variable. https://developer.android.com/topic/libraries/architecture/room.html Even if you are 'only' needing to store a global variable and it's no big deal and what not, but using the Room database is the most elegant, native and well supported way of keeping values around the life cycle of the activity. It will help to prevent many issues, especially integrity of data. I understand that database and global variable are different but please use Room for the sake of code maintenance, app stability and data integrity.

有几种不同的方法可以达到你的要求。

1)。扩展应用程序类并在那里实例化控制器和模型对象。

public class FavoriteColorsApplication extends Application {

    private static FavoriteColorsApplication application;
    private FavoriteColorsService service;

    public FavoriteColorsApplication getInstance() {
        return application;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        application = this;
        application.initialize();
    }

    private void initialize() {
        service = new FavoriteColorsService();
    }

    public FavoriteColorsService getService() {
        return service;
    }

}

然后你可以在任何时候从你的自定义Application对象中调用你的单例:

public class FavoriteColorsActivity extends Activity {

private FavoriteColorsService service = null;
private ArrayAdapter<String> adapter;
private List<String> favoriteColors = new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_favorite_colors);

    service = ((FavoriteColorsApplication) getApplication()).getService();
    favoriteColors = service.findAllColors();

    ListView lv = (ListView) findViewById(R.id.favoriteColorsListView);
    adapter = new ArrayAdapter<String>(this, R.layout.favorite_colors_list_item,
            favoriteColors);
    lv.setAdapter(adapter);
}

2)。你可以让你的控制器只创建一个它自己的单例:

public class Controller {
    private static final String TAG = "Controller";
    private static sController sController;
    private Dao mDao;

    private Controller() {
        mDao = new Dao();    
    }

    public static Controller create() {
        if (sController == null) {
            sController = new Controller();
        }
        return sController;
    }
}

然后你可以从任何Activity或Fragment调用create方法,如果一个控制器不存在,它会创建一个新的控制器,否则它会返回先前存在的控制器。

3)。最后,在Square上创建了一个灵活的框架,为你提供Android内部的依赖注入。它叫匕首。我不会在这里详细说明如何使用它,但如果你需要这类东西,它是非常灵巧的。

我希望我已经给出了足够的细节,告诉你如何去做你所希望的事情。

你可以扩展基本的android.app.Application类,并像这样添加成员变量:

public class MyApplication extends Application {

    private String someVariable;

    public String getSomeVariable() {
        return someVariable;
    }

    public void setSomeVariable(String someVariable) {
        this.someVariable = someVariable;
    }
}

在你的android manifest中,你必须声明实现android.app. application的类(添加android:name="。属性到现有的应用程序标签):

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

然后在你的活动中,你可以像这样获取和设置变量:

// set
((MyApplication) this.getApplication()).setSomeVariable("foo");

// get
String s = ((MyApplication) this.getApplication()).getSomeVariable();

我检查了类似的答案,但这里给出的不符合我的需要。 我找到了,在我看来,正是你要找的东西。唯一可能的黑点是安全问题(也可能不是),因为我不了解安全问题。

我建议使用Interface(不需要使用带有构造函数的Class,所以…),因为你只需要创建如下内容:

public interface ActivityClass {

    public static final String MYSTRING_1 = "STRING";

    public static final int MYINT_1 = 1;

}

然后你可以使用以下命令访问类中的任何地方:

int myInt = ActivityClass.MYINT_1;
String myString = ActivityClass.MYSTRING_1;

使用SharedPreferences存储和检索全局变量。

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
String userid = preferences.getString("userid", null);