在 Android 应用中的活动之间传递数据

Passing Data Between Activities in Android App

嘿,我是制作 android 应用程序的新手,我知道在两个 Activity 之间传递数据的最简单方法是通过 Intent。

在我的一个 classes (EventOptions.java) 中,我将这行代码称为:

Intent i = new Intent(EventOptions.this, PhotoFetcher.class);
i.putExtra("imageArray", imageIDs); 
startActivity(i);

imageIDs 是一个字符串数组

在我的 PhotoFetcher class 中,我想将一个名为 imageIDs 的字符串数组设置为我正在通过 Intent 传递的 imageIDs 字符串数组。

我想在 class:

中将图像设置为全局变量
public class MainActivity extends Activity implements View.OnClickListener{

    Intent it = getIntent();
    String[] imageIDs = it.getStringArrayExtra("imageArray");
    ...
}

但是这会使我的应用程序崩溃。这是不允许的吗?如果是这样,我该如何解决?提前致谢!

需要在方法中而不是在 class 级别调用 getIntent()。在 onCreate 中调用它:

public void onCreate(Bundle savedInstanceState){
  super.onCreate(savedInstanceState);
   // get Intent here
    Intent it = getIntent();
    String[] imageIDs = it.getStringArrayExtra("imageArray");
}

if I want to use the imageIDs array in another public class defined in my PhotoFetcher class, do I need to call it again?

要在 PhotoFetcher class 中获取 imageIDs,要么将 String[] imageIDs 声明为全局变量,要么使用 PhotoFetcher imageIDs class构造函数

你必须使用 putStringArrayListExtra。您可以先将 String[] 转换为 ArrayList。

像这样

ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(imageIDs));
Intent i = new Intent(EventOptions.this, PhotoFetcher.class);
i.putStringArrayListExtra("imageArray", arrayList); 
startActivity(i);

然后您可以像以前一样获取它,最好是在 onCreate 中或调用之后。

Intent it = getIntent();
ArrayList<String> imageIDs = it.getStringArrayListExtra("imageArray");

共享数据而不持久化到磁盘

可以通过将数据保存在内存中来在活动之间共享数据,因为在大多数情况下,两个活动 运行 在同一进程中。

注意:有时,当用户离开您的 activity(没有退出)时,Android 可能会决定终止您的应用程序。在这种情况下,我遇到过 android 尝试使用应用程序被杀死之前提供的意图启动最后一个 activity 的情况。在这种情况下,存储在单例(您的或应用程序)中的数据将消失,并且可能会发生坏事。为避免这种情况,您要么将对象持久化到磁盘,要么在使用前检查数据以确保其有效。

使用单例class

有一个class来整理数据:

public class DataHolder {
  private String data;
  public String getData() {return data;}
  public void setData(String data) {this.data = data;}

  private static final DataHolder holder = new DataHolder();
  public static DataHolder getInstance() {return holder;}
}
From the launched activity:

String data = DataHolder.getInstance().getData();

使用应用程序单例(我会推荐这个)

应用程序单例是 android.app.Application 的一个实例,它是在应用程序启动时创建的。您可以通过扩展应用程序来提供自定义应用程序:

import android.app.Application;
public class MyApplication extends Application {
  private String data;
  public String getData() {return data;}
  public void setData(String data) {this.data = data;}
}

启动 activity 之前:

MyApplication app = (MyApplication) getApplicationContext();
app.setData(someData);

然后,从发射activity:

MyApplication app = (MyApplication) getApplicationContext();
String data = app.getData();

ρяσѕρєя K 正中要害,你是 运行 构造函数和字段所在的方法。要使变量(imageID)成为全局变量,这很简单,有几种方法可以做到。在任何方法之外声明它们,然后在您的 onCreate 或 onResume 中分配它们(将始终被调用)。

试试这个:

public class MainActivity extends Activity implements View.OnClickListener {
 //global variable
String[] imageIDs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // get Intent here
        Intent it = getIntent();
        imageIDs = it.getStringArrayExtra("imageArray");
    }

}