Search code examples
androidandroid-intentandroid-activityandroid-pendingintent

Return to parent activity from child activity when reopening from Android history


My application allows the users to browse around in certain child activities FileListActivity owned by a parent MainActivity. When the user clicks e.g. home button, the app goes to background.

Problem:

When the user opens the app via PendingIntent or from history, the child activity FileListActivity is shown. Pressing the back button from here closes the app (sends app to background).

Visualized flow:

  1. Open app
  2. MainActivity opens
  3. user opens child FileListActivity
  4. user clicks home button
  5. user opens app from Android history
  6. user clicks back button

App goes to background, but FileListActivity should close and return to MainActivity

Question:

Using something like

startActivity(new Intent(MainActivity.this, FileListActivity.class));

How can I ensure that when the user reopens the app onto the child activity and presses the back button that the app returns to MainActivity and doesn't go to background?

I know I should be using IntentFlags, just not sure which to use.


Solution

  • FLAG_ACTIVITY_RETAIN_IN_RECENTS may do the trick

    Or

    You startActivityForResult instead of startActivity.

    refer the android dev for more info here.