Pergunta

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?

Foi útil?

Solução

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

Outras dicas

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

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top