30

I am using moveTaskToBack() method in my activity.

When I am pressing back button the activity still is visible. The back key does not work. What did I do wrong here? I want to navigate back to the previous activity.

public void onBackPressed() {
    // TODO Auto-generated method stub
    moveTaskToBack(true);
    new Finalizer().killApp(false);
    super.onBackPressed();
}
genpfault
  • 51,148
  • 11
  • 85
  • 139
Dinesh
  • 965
  • 2
  • 11
  • 23
  • I can say it causes blank black screen for 1s when you are closing apk (if it was the last activity before exit of course). – deadfish Apr 08 '13 at 12:01

3 Answers3

18

The purpose of moveTaskToBack:

http://developer.android.com/reference/android/app/Activity.html

moveTaskToBack(boolean nonRoot)

Move the task containing this activity to the back of the activity stack.

What you could do is:

public void onBackPressed() {
    moveTaskToBack(true); // I don't think you're looking for this.
    new Finalizer().killApp(false); // Neither this.
    super.onBackPressed();
}

Maybe this: Android: Go back to previous activity Something similar in your onBackPressed() after super.OnBackPressed(); Also make sure when you're hitting your back button, you're actually going in this method.

Community
  • 1
  • 1
Noman Arain
  • 1,172
  • 4
  • 19
  • 45
4

If you call super it triggers the default implementation of back button, which just finishes the current activity. Try removing the line super.onBackPressed(); and then see if the new behavior is what you expect.

Community
  • 1
  • 1
Josh Bibb
  • 477
  • 4
  • 14
4

Before i update the code, when i want to close my app in these situations, something unexpected happen to me.

Launch -> isSignIn(yes) -> MainActivity -> Press Back -> Close.


Launch -> isSignIn(no) -> SignInActivity -> Press Back -> Close.


Launch -> isSignIn(yes) -> MainActivity -> Press Sign Out -> SignInActivity -> Press Back -> SignInActivity -> Press Back -> SignInActivity (and so on loop and stuck on SignInActivity until i press history/recent app/home button).


Launch -> isSignIn(no) -> SignInActivity -> Press Sign In -> MainActivity -> Press Sign Out -> SignInActivity -> Press Sign In -> MainActivity -> Press Back -> MainActivity -> Press Back -> MainActivity (and so on loop and stuck on MainActivity until i press history/recent app/home button).

Now i provide this code both on SignInActivity that has Sign In button and MainActivity that has Sign Out button.

@Override
    public void onBackPressed() {
        super.onBackPressed();
        moveTaskToBack(true);
        finish();
    }

It's now working fine on every situations. When i press Back, the application close.

nandur
  • 134
  • 10