0

I have a subActivity that can be open from my mainActivity.

For some reasons, when the user clicks on the back button go back to my mainActivity, I want my subActivity to remain open in background in order to be able to come back for later.

Questions:

  • how to avoid to close the subActivity when the user clicks back ?
  • how to come back to the mainActivity without restarting it ?
  • how to come back later to my opened activity without re-creating it completely ? (just want to bring it to front)

Thanks !

1
  • Don't finish your activity just change. Commented Nov 22, 2018 at 10:55

2 Answers 2

3

On you subActivity onBackPressed() add this

@Override public void onBackPressed() { Intent i = new Intent(SubActivity.this, MainActivity.class); i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); startActivity(i); } 

on mainActivity :

 private void openSubActivity() { Intent intent = new Intent(MainActivity.this,SubActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); startActivity(intent); } 
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks but how to come back to the mainActivity without restarting it ?
Ok, probably found the answer stackoverflow.com/questions/8688099/… for this last point
I modified your answer with the solution ! Thanks a lot.
0

override onBackPressed() and remove super from it then try opening the activity which you want to open from there for ex-

 @Override public void onBackPressed() { // your code } 

and with the help of different activity launch modes you can achieve it

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.