moveTaskToBack(true) returns false always

2020-02-11 09:02发布

问题:

I know this has been asked earlier here & here too.They are not answered properly (or not answered at all).But,i don't know why moveTaskToBack(true); always returns false for me .Can anyone tell me why and how could i solve the issue? Thanks in advance.

public void onBackPressed() {       
    boolean r=  moveTaskToBack(true);
    //r is false !! using API LEVEL 8
}

Note:The activity from which i am calling it is a child Activity included in a tabActivity and neither of this is a MAIN or LAUNCHER activity.I don't know if that makes a difference.

EDIT: and as a result the application does'nt go to background.I want it to go to background just as if the hardware HOME is pressed

回答1:

I don't know why moveTaskToBack(true) is returning false for you. Perhaps there's something weird in your manifest? At any rate, you can do this instead to bring up the home screen:

Intent i = new Intent();
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
this.startActivity(i);

However, be aware of this message:

"You cannot simulate a press on the Home key." — Roman Guy, Android framework engineer

I'm not sure how that squares with my suggested code (which I found on the same thread as Roman's statement and seems to work).



回答2:

I came into the similar problem(a child Activity included in a tabActivity), when you call moveTaskToBack(true) in the child activity, it doesn't work, whereas it works in the parent activity. You can call moveTaskToBack(true) in child activity like this: ChildActivity.this.getParent().moveTaskToBack(true)



回答3:

Just write:

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