如何通过使用意图活动之间的操作对象 [英] How to pass and manipulate objects between activities using intents

查看:278
本文介绍了如何通过使用意图活动之间的操作对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我与Android的第一个问题lifecycleand我觉得多少有些无奈:

This is my first issue with the Android lifecycleand I feel somewhat helpless:

在活动的 A 的存在的OnCreate。这就是我创建一个名为 playerNames 的一个ArrayList现货和ArrayList所谓的移动的。也有一些更多的东西在OnCreate中发生的事情。在A的在onStart我创建了一个标志,所以我知道这活动是在运行的情况下,我想关闭所有一次。在对的onDestroy标志设置回空。

In Activity A there's onCreate. That's the spot where I create an ArrayList called playerNames and ArrayList called moves. Also there's some more stuff happening in oncreate. In A's onStart I create a flag so I know which Activity is running in case I'd like to close all at once. In onDestroy the flag is set back to null.

最后我做的意图,相处到活动的的,我拿的移动列表中。工作正常。

Eventually I make an intent to get to Activity B where I take the moves list along. Works fine.

现在我想打从B中的意图要回A.当我尝试,在生命周期中会发生什么?显然,A的的onCreate被称为并导致一个NullPointerException异常regrding中的 playerNames 列表中。

Now I'd like to make an intent from B to get back to A. What happens in the lifecycle when I attempt that? Obviously onCreate of A is called and leads to a NullPointerException regrding the playerNames list.

我想,而B正在运行存储这个ArrayList和拿回来,当我回来A.哪种方法是正确的(onResume?onRestart?),如何保存呢?我真的需要共享preferences?

I'd like to store this ArrayList while B is running and get it back when I come back to A. Which method is the right one (onResume? onRestart?) and how do I store it? Do I really need SharedPreferences?

预先感谢你的帮助。

推荐答案

在活动答:

- 创建要启动一个Intent,指定至极活动

-Create an Intent, specifying wich activity you want to start

-put数据。

-Start活性的一个结果。

-Start Activity for a result.

-On 的onActivityResult()验证您的数据接收和你想用它的东西。

-On onActivityResult() verify that you have data to receive and do what you want with it.

在活动B:

在-On 的onCreate()收到活动A的数据;

-On the onCreate() receive the data from Activity A;

-modify数据如你所愿;

-Modify the data as you wish;

- 创建一个新的意图;

-Create a new Intent;

-put在Intent中的数据;

-Put the data in the Intent;

- 设置活动结果和意图数据。

-Set the activity result and the intent data.

-Finish活动B

-Finish activity B

下面是我描述的步骤的样本,它的作用是有活性的启动活动B,并将其传递一个空的ArrayList。然后在活动B,ArrayList中填充并送回到ArrayList的内容,屏幕上显示活动A。

Below is a sample of the steps I describe, what it does is have activity A start an Activity B, and passing it an empty ArrayList. Then on Activity B, the arrayList is populated and sent back to Activity A where the contents of the arrayList, are displayed on screen.

code:

活动答:

private static final int REQUEST_LIST = 1;
ArrayList<String> myList;
TextView listText;

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

    listText = (TextView) findViewById(R.id.mylist_text);
    myList = new ArrayList<String>();

    Intent i = new Intent(MainActivity.this, ActivityB.class);
    i.putExtra(ActivityB.EXTRA_ARRAY, myList);
    startActivityForResult(i, REQUEST_LIST);

}

@Override
protected void
        onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode != Activity.RESULT_OK) return;

    if (requestCode == REQUEST_LIST) {
        myList = (ArrayList<String>) data.getSerializableExtra(ActivityB.EXTRA_ARRAY);

        StringBuilder text = new StringBuilder();
        for (int j = 0; j < myList.size(); j++) {
            text.append(myList.get(j) + " ");
        }

        listText.setText(text.toString());
    }
}

Activty乙:

public static final String EXTRA_ARRAY = "com.example.androidtest.mainactivity.array";

ArrayList<String> myList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activityb_layout);
    myList = (ArrayList<String>) getIntent().getSerializableExtra(EXTRA_ARRAY);

    for (int i = 0; i < 10; i++) {
        myList.add(String.valueOf(i));
    }

    Intent data = new Intent();
    data.putExtra(EXTRA_ARRAY, myList);
    setResult(Activity.RESULT_OK, data);

    finish();
}

注意::你不能忘记在manifest文件中声明你的活动B。另外要对活动如何知道哪些数据关注时发送和收集,通过类,至极必须是一致的,所以要避免使用文字字符串常量创建和使用定义的常量。

Attention: You cannot forget to declare your Activity B in the manifest File. Also pay attention on how the activities know what data to send and collect, through constants created in the classes, wich must be consistent, so avoid using literal strings, and use defined constants.

这篇关于如何通过使用意图活动之间的操作对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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