【问题标题】:How to read variable from the other Activity如何从其他活动中读取变量
【发布时间】:2017-03-20 09:05:54
【问题描述】:

我有两个活动。(活动 1 和活动 2) 活动 1 有一个这样的公共数组

 public byte[][] arrtest=new byte[3][1024];

我调用活动 2,我想从活动 2 中的 arrtest 中获取价值。

我该怎么做?

【问题讨论】:

  • 可以intent传递。
  • 通过intent传递arrtest数组
  • 在捆绑包或意图中传递它或将其声明为静态然后在任何地方使用它
  • 如果尺寸太大,Intent 将不起作用。把它保存在某个地方。然后将路径传递给另一个Activity
  • 使用共享偏好

标签: android bundle


【解决方案1】:

您必须使用捆绑包或将其添加到意图中。

    Intent intent = new Intent(view.getContext(), ApplicationActivity.class);
                        intent.putExtra("key", value);
                        startActivity(intent);

调用的活动。

Intent intent = getIntent();
Bundle bundle = intent.getExtras();

if(bundle != null){
    mealId = bundle.getInt("key");
}

【讨论】:

    【解决方案2】:

    将数组传递给另一个活动,然后使用它。查看这个答案

    https://stackoverflow.com/a/7257726/3789993

    【讨论】:

    • 非常感谢 AbhayBohra
    • @AbhayBohra 如果字节数组大小超过 500 kb 怎么办?此解决方案行不通。
    • 使用单例类。您将数组保存在单例变量中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-26
    • 1970-01-01
    • 1970-01-01
    • 2012-08-25
    • 2023-01-18
    • 1970-01-01
    相关资源
    最近更新 更多