Android的基本活动:基本的全局变量,无法从某些activites获得基本、全局变量、Android、activites

2023-09-04 23:21:36 作者:水波、美丽的无法言语ァ

我是在一个机器人类现在,让我有点新的Andr​​oid应用程序开发。

I'm taking an android class now, so I am somewhat new to android app development.

为基本活动我的第一个假设是,它的全局变量,它的值将提供给所有活动。我发现,它是提供给我的主要活动,但没有任何活动之后。

My first assumption for a Base Activity is that it's Global Variables and it's values would be available to all activities. I have found that it is available to my Main Activity, but not any activities after that.

在基本活动,我存储对象的ArrayList。我还从没有一个XML,增加对象到ArrayList加载数据。一旦在主要活动我仍然可以访问该ArrayList和它的价值。我用它来填充列表。但是,当我去到下一个活动,它知道该ArrayList,但他认为它是空的。

In the Base Activity I am storing an ArrayList of Objects. I also load data from an xml in there that adds objects to the arrayList. Once in the Main Activity I still have access to that arrayList and it's values. I use it to fill a list. But when I go to the next activity, it knows about the arrayList but thinks it is empty.

我是否需要建立在基活性的方法来检索ArrayList和对象添加到数组列表?

Do I need to create methods in the base activity to retrieve the arrayList and to add objects to the array list?

任何帮助将是AP preciated。

Any help would be appreciated.

感谢您,

米歇尔

推荐答案

全局变量需要声明静态。然后,他们会在任何类访问。例如:

Global variables need to be declared static. Then they would be accessible from any class. Example:

public class Globals {
    public static String myString;
}

任何类都可以读/写这样的myString的:

Any class can read/write the myString like this:

Globals.myString = "foo";

String bar = Globals.myString;