Skip to content Skip to sidebar Skip to footer

How To Pass And Manipulate Objects Between Activities Using Intents

This is my first issue with the Android lifecycleand I feel somewhat helpless: In Activity A there's onCreate. That's the spot where I create an ArrayList called playerNames and Ar

Solution 1:

On Activity A:

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

-Put data on that intent to be received by activity B.

-Start Activity for a result.

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

On Activity B:

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

-Modify the data as you wish;

-Create a new Intent;

-Put the data in the Intent;

-Set the activity result and the intent data.

-Finish activity B

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:

Activity A:

privatestaticfinalintREQUEST_LIST=1;
ArrayList<String> myList;
TextView listText;

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

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

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

}

@OverrideprotectedvoidonActivityResult(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);

        StringBuildertext=newStringBuilder();
        for (intj=0; j < myList.size(); j++) {
            text.append(myList.get(j) + " ");
        }

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

Activty B:

publicstatic final StringEXTRA_ARRAY = "com.example.androidtest.mainactivity.array";

ArrayList<String> myList;

@OverrideprotectedvoidonCreate(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 = newIntent();
    data.putExtra(EXTRA_ARRAY, myList);
    setResult(Activity.RESULT_OK, data);

    finish();
}

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.

Solution 2:

From Activity B you can come back to the previous A instance using:

Intent intent = newIntent(this, A.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);

This will clear all current stack and go back to the A instance ( http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP ). As long as your Activity doesn't get destroyed, you'll have all data there.

Anyway, you should consider use Activity's save instance state capability ( http://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState%28android.os.Bundle%29 ).

Post a Comment for "How To Pass And Manipulate Objects Between Activities Using Intents"