I work on an application where ActivityA starts ActivityB with
'startActivityForResult'. Overall, this seems to work fine, but there
are two things I do not understand:
1. After ActivityB executes finish(), the *first* method called in
ActivityA is onActivityResult with the correct result. But ActivityA
is still in stopped state and therefore does not have its current
state available. Shouldn't it be that the correct sequence is:
ActivityA.onStart ... ActivityA.onResume...
ActivityA.onActivityResult??? Btw: both activities have default launch
modes.
2. Before ActivityB gets executed, ActivityA.onSaveInstanceState is
executed (and others of course) so that it can make its state
persistent. However, after ActivityB is finished
ActivityA.onRestoreInstanceState and/or ActivityA.onCreate are not
called so that the saved state is lost and the activity has no chance
to restore its previous state. I can't see any reason why this is not
the case. Could anyone give me some light on this issue?
Thanks
Danny
The source code (reduced to the min) is as follows:
public class ActivityA extends Activity {
public static final int ACTIVITYB = 0x20000001;
public static final String INTENT_BACK_ARGUMENT =
"ActivityBToAArgument";
public final static String INTENT_ARGUMENT = "ActivityAToBArgument";
private Intent activityResult = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new AbsoluteLayout(this));
}
protected void onStart() {
super.onStart();
handleActivityResults();
}
@Override
protected void onRestart() {
super.onRestart();
setContentView(new AbsoluteLayout(this));
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onPause() {
super.onPause();
onSaveInstanceState(new Bundle());
}
@Override
protected void onStop() {
super.onStop();
}
@Override
protected void onDestroy() {
super.onDestroy();
}
@Override
protected void onSaveInstanceState(Bundle bundle) {
super.onSaveInstanceState(bundle);
bundle.putString("test", "testvalue");
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
String val = savedInstanceState.getString("test");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.addSubMenu(0, 1, 0, "Activity B");
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
Intent openFileIntent = new Intent(this, ActivityB.class);
openFileIntent.putExtra(INTENT_ARGUMENT, "Test");
startActivityForResult(openFileIntent, ACTIVITYB);
return true;
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
activityResult = data;
}
}
private void handleActivityResults() {
if (activityResult != null) {
finishActivity(ACTIVITYB);
String msg = activityResult.getExtras().getString
(INTENT_BACK_ARGUMENT);
activityResult = null;
}
}
}
public class ActivityB extends Activity {
private static final int MENU_TEST = 1;
private String argument = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new AbsoluteLayout(this));
argument = getIntent().getExtras().getString
(ActivityA.INTENT_ARGUMENT);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
setResult(RESULT_CANCELED, null);
finish();
return true;
}
return false;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, MENU_TEST, 0, argument);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent intent = new Intent();
intent.putExtra(ActivityA.INTENT_BACK_ARGUMENT, "Seems ok");
setResult(RESULT_OK, intent);
finish();
return true;
}
}
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---