1

Imagine that you have 2 activitie - A and B. You're starting B from A. In B you have a task. And if task is completed you want to go back to A when back key pressed if not you want to dismiss the whole application. But after dissmissing you can resume B from recent apps menu. And if after that you need the same behavior of back key.

So I want to dismiss just one activity or the whole app (but with resume possibility). Is there any way to achive that?

Lingviston
  • 5,479
  • 5
  • 35
  • 67

2 Answers2

1

In ActivityB override onBackPressed() and if the task is completed, just call super.onBackPressed() which will finish ActivityB and return to ActivityA. Otherwise, to dismiss the whole app just use moveTaskToBack() and don't call super.onBackPressed(). When the user returns to the app, ActivityB will be shown (since it has not yet been finished).

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • Yes, but if moveTaskToBack() is called then when task will complete and I'll press back key the whole application still will be dismissed. And there is no way to move activity B to the front of the stack as far as I know. – Lingviston May 31 '13 at 07:09
  • Sorry, don't understand. If your activity stack contains `ActivityA->ActivityB` and you move this task to the background, then bring it back to the foreground, you still have `ActivityA->ActivityB` with `ActivityB` in front. Please explain more what you are doing. – David Wasser May 31 '13 at 08:20
0

To dismiss an Activity, you can call its finish() method. And for dismissing the whole application, try launching an Intent for the Home Activity. Take a look to this: How to Launch Home Screen Programmatically in Android

Community
  • 1
  • 1
Javier Enríquez
  • 630
  • 1
  • 9
  • 25