What is the purpose of using moveTaskToBack() method in android?

前端 未结 3 509
不思量自难忘°
不思量自难忘° 2020-12-09 02:42

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

3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-09 03:01

    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.

提交回复
热议问题