Have an activity that extends application, in here you need two flag's.
Declaring Global Variables
public class MyApplication extends Application {
public static boolean flagForHome= false;
public static boolean flagForChangingActivity= false;
}
Then in onPause and onResume of each activity
Activity Lifecycle
public void onResume(){
if(flagForHome && !flagForChangingActivity){
finish();
} else {
MyApplication.flagForHome= false;
MyApplication.flagForChangingActivity= false;
}
}
public void onPause(){
MyApplication.flagForHome= true;
}
Finally when changing activity normally using startActivity(); you will need to set the flagForChangingActivity to true;
Using Intents
MyApplication.flagForChangingActivity= true;
Intent intent = new Intent(this, ActivityTwo.class);
startActivity(intent);
this will stop your app closing when changing activity (as the activity going into the background will hit onPause and the new activity starting up will hit onResume)
Don't forget in your home activity to set the flag back to false. This will stop your app instantly closing itself when you start it the second time!
HomeActivity.class:
public void onPause(){
Application.flagForHome= false;
}
EDIT
Start Activity for Result is just the same procedure:
MyApplication.flagForChangingActivity= true;
Intent intent = new Intent(this, ActivityTwo.class);
startActivityForResult(intent, 0);