4

Having trouble passing data between activities. ListActivity is collecting data and when back button is pressed returns to MainActivity and then want to get that data via onResume method but I don't 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));
    }
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Palaima
  • 331
  • 7
  • 17

3 Answers3

4

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:

Launch an activity for which you would like a result when it finished. When this activity exits, your onActivityResult() method will be called with the given requestCode. Using a negative requestCode is the same as calling startActivity(Intent) (the activity is not launched as a sub-activity).

Ben Weiss
  • 17,182
  • 6
  • 67
  • 87
2

You'll want to start the activity with startActivityForResult() and override onActivityResult() to handle the data you return from the second Activity.

Check out this article on the Android developers site for more info.

Robby Pond
  • 73,164
  • 16
  • 126
  • 119
2

Perhaps you should

This will bring you back to your previous MainActivity. So how do you retrieve the data set by your ListActivity?

There's a brief explanation of this mechanism in Starting Activities and Getting Results.

villoren
  • 319
  • 3
  • 5