问题描述
有活动之间传递数据的麻烦ListActivity正在收集数据,并且在后退按钮pressed返回MainActivity,然后想通过onResume方法的数据,但我不明白什么
这怎么解决?
ListActivity.java
@覆盖
公共无效结束(){
I =新的意图(ArrayListActivity.this,MainActivity.class);
i.putParcelableArrayListExtra(Constants.TAG_SELECTED_PRODUCT_LIST,selected_list);
super.finish();
}
MainActivity.java
@覆盖
保护无效onResume(){
super.onResume();
捆绑额外= getIntent()getExtras()的getBundle(Constants.TAG_SELECTED_PRODUCT_LIST)。
如果(临时演员!= NULL){
selected_list = extras.getParcelableArrayList(Constants.TAG_SELECTED_PRODUCT_LIST);
myListView.setAdapter(新ProductAdapter(MainActivity.this,
R.layout.array_lisviewt_item_row,selected_list));
}
}
您可能想从第一个通过启动第二个活动的 startActivityForResult(...)
的方法。
这个方法可以让你从一个启动的活动运送结果返回给它的发射活动。
从文档:
Having trouble passing data between activitiesListActivity is collecting data and when back button is pressed returns to MainActivity and then want to get that data via onResume method but i dont get anything
How can this problem be solved?
ListActivity.java
@Override
public void finish() {
i = new Intent(ArrayListActivity.this, MainActivity.class);
i.putParcelableArrayListExtra(Constants.TAG_SELECTED_PRODUCT_LIST, selected_list);
super.finish();
}
MainActivity.java
@Override
protected void onResume() {
super.onResume();
Bundle extras = getIntent().getExtras().getBundle(Constants.TAG_SELECTED_PRODUCT_LIST);
if(extras != null) {
selected_list = extras.getParcelableArrayList(Constants.TAG_SELECTED_PRODUCT_LIST);
myListView.setAdapter(new ProductAdapter(MainActivity.this,
R.layout.array_lisviewt_item_row, selected_list));
}
}
You probably want to start your second activity from the first one via the startActivityForResult(...)
method.
This method allows you to transport results from a launched activity back to it's launching activity.
From the documentation:
这篇关于发送这是在研究背景运行数据活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!