Search code examples
androidonactivityresult

Trying to use moveTaskToBack to background my app if the user selects cancel on a popup, but onActivityResult is called many times


Here's my code:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode == LOGIN_REQUEST) {
        if(resultCode == RESULT_OK) {
            //Do Stuff
        } else if(resultCode == RESULT_CANCELED) {
            moveTaskToBack(true);
        }
        return;
    }
    super.onActivityResult(requestCode, resultCode, data);
}

After moveTaskToBack(true) my app is backgrounded, but onActivityResult gets called every time I try to open my app, which immediately backgrounds it again. How do I signal that I've handled the activity result and don't want to be notified about it again?

It should be noted, that I tried this with

 super.onActivityResult(requestCode, resultCode, data);

being the first thing in the method as well, same problem.


Solution

  • You probably need something like this, instead of doing it on the onActivityResult method:

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