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 !

Regis_AG
  • 12,634
  • 20
  • 90
  • 174

2 Answers2

2

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);

    }
Regis_AG
  • 12,634
  • 20
  • 90
  • 174
Ali Ahmed
  • 2,050
  • 1
  • 12
  • 19
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

ʍѳђઽ૯ท
  • 16,177
  • 7
  • 51
  • 106
Rohit Sharma
  • 1,284
  • 10
  • 18