views:

164

answers:

2

I need to have two activities in an Android app that can be switched between each other with UI persistence as follows:

  1. Activity A launches Activity B.

  2. User triggers some UI changes in Activity B.

  3. Activity B returns to Activity A (by a call to onBackPressed() or something similar)

  4. Activity A re-launches Activity B.

I would like the changes made in step 2 to be visible in step 4.

I have tried using the singleInstance activity tag on Activity B to no avail. I would also prefer a more elegant solution than simply writing all object properties to a file or SQLite table. It seems that this behaviour must be easily achievable given that Android does it automatically for calls to onBackPressed() where the parent Activity's UI is saved.

Any help is much appreciated.

+1  A: 

Activity A launches Activity B

.

Intent intent = new Intent(ActivityA.this, ActivityB.class);
startActivityForResult(intent); 

Launch Activity B from Activity A using startActivityForResult.

User triggers some UI changes in Activity B

.

Activity B returns to Activity A (by a call to onBackPressed() or something similar)

Intent intent = new Intent();
                    intent.putExtra("change_value1", change1);
                    intent.putExtra("change_value2", change2);
                    setResult(RESULT_OK, intent);
                    finish();

Activity A re-launches Activity B.

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        /*
         * Gets invoked on finish() from ActivityB.class
         */
        super.onActivityResult(requestCode, resultCode, data);

        switch(requestCode){
        case 0:

            if(resultCode == RESULT_OK){
                String change1, change2;
                    change1 = data.getStringExtra("change_value1");
                change2 = data.getStringExtra("change_value2");
                Intent intent = new Intent(ActivityA.this, ActivityB.class);
                                startActivity(intent); 
            }
        }
    } 

You can read more about intents here.

primalpop
Thanks for your response. I was still hoping for a cleaner approach. Intuitively, I expect one to exist because of the way android handles switching back to the parent activity. I suppose this is the best way to go for now then. Thanks for your help.
aandroid
@aandroid - It's pretty intuitive if you're familiar with event handling. You're still launching the activity via an `Intent`, nothing new there. Activity A is simply handling the results of activity B via a callback handler.
Matt Huggins
A: 

If you want to persist data between multiple activities it's best to create a subclass of Application and put your objects in there. Then each activity you spawn can get hold of the same objects.

http://developer.android.com/reference/android/app/Application.html

http://stackoverflow.com/questions/708012/android-how-to-declare-global-variables

James