Let's say that from the activity A, the activity B is launched and from B is launched C. Is it OK to start the activity A from C, by pressing a butto开发者_如何学Pythonn, instead of pressing the BACK button twice, or is it a memory leak practice?
Intent.FLAG_ACTIVITY_CLEAR_TOP
You must to do that:
`Activity A:
public static final int ID = X
// ....
startActivityForResult(intentToB,ID)
// ....
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == ID){
switch (resultCode){
case RESULT_OK:
//...
break;
case RESULT_RETURN:
// ...
default:
//...
}
}
}
`
` Activity B:
public static final int ID = Y
// ....
startActivityForResult(intentToC,ID)
// ....
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == ID){
switch (resultCode){
case RESULT_OK:
//...
break;
case RESULT_RETURN:
setResult(RESULT_RETURN);
ActivityB.this.finish();
default:
//...
}
}
}
`
Activity C:
//inside of button onclick listener
setResult(RESULT_VOLVER);
ActivityC.this.finish();
I understand your idea. It seems from HomeActivity, you enter Activity A, and next to Activity B, and so one. Imaging each Acitivy (A, B,...), there is an HOME button for quickly return to HOME when press this button.
Here is the source code:
public void btn_home_click(View v) {
Intent intent = new Intent(this, HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
}
精彩评论