如何使用意图在活动之间传递和操作对象
这是我对Android生命周期的第一个问题,我感到有些无奈:
This is my first issue with the Android lifecycleand I feel somewhat helpless:
在活动 A 中有 onCreate.这就是我创建名为 playerNames 的 ArrayList 和名为 moves 的 ArrayList 的地方.oncreate 中还发生了更多的事情.在 A 的 onStart 中,我创建了一个标志,以便我知道哪个活动正在运行,以防我想一次关闭所有活动.在 onDestroy 中,标志被设置回 null.
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.
最终我打算进入活动 B,在那里我带着 moves 列表.工作正常.
Eventually I make an intent to get to Activity B where I take the moves list along. Works fine.
现在我想从 B 发出一个返回 A 的意图.当我尝试这样做时,生命周期中会发生什么?显然,A 的 onCreate 被调用并导致 playerNames 列表的 NullPointerException.
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?),我该如何存储它?我真的需要 SharedPreferences 吗?
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?
预先感谢您的帮助
关于活动 A:
-创建一个意图,指定你想要开始的活动
-Create an Intent, specifying wich activity you want to start
-将有关该意图的数据放入活动 B 中.
-Put data on that intent to be received by activity B.
-开始活动以获得结果.
-Start Activity for a result.
-On onActivityResult()
验证您有数据要接收并用它做您想做的事.
-On onActivityResult()
verify that you have data to receive and do what you want with it.
关于活动 B:
-在onCreate()
上接收来自Activity A的数据;
-On the onCreate()
receive the data from Activity A;
-根据需要修改数据;
-创建一个新的意图;
-将数据放入Intent;
-Put the data in the Intent;
-设置活动结果和意图数据.
-Set the activity result and the intent data.
-完成活动 B
下面是我描述的步骤示例,它的作用是让活动 A 启动一个活动 B,并传递给它一个空的 ArrayList.然后在 Activity B 上填充 arrayList 并将其发送回 Activity A,其中 arrayList 的内容显示在屏幕上.
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.
代码:
活动 A:
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());
}
}
活动 B:
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();
}
注意:您不能忘记在清单文件中声明您的活动 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.