Skip to content Skip to sidebar Skip to footer

Background A React-native Android App Using Back Button

I've followed the example pattern for handling the android back button in the react-native docs and it works well. I can use the hardware back button to pop my navigation stack.

Solution 1:

Answered my own question. The trick is to override the default back button behaviour in the MainActiviy:

publicclassMainActivityextendsReactActivity {

    @OverrideprotectedStringgetMainComponentName() {
        return"foo";
    }

    @OverridepublicvoidinvokeDefaultOnBackPressed() {
        // do not call super. invokeDefaultOnBackPressed() as it will close the app.  Instead lets just put it in the background.moveTaskToBack(true);
    }
}

Solution 2:

Though I may be very late in giving the answer it may help other facing the issue.

Recently I came across the same requirement where I have to move the app to the background. I tried the solution provided by @pomo. Though it worked I faced problems. Sometimes on multiple clicking of the back button, the app misbehaves in android though it worked perfectly fine in iOS.

And then I came across the following issues in GitHub where it mentions the reason for the misbehaviour.

The following solution works perfectly fine now.

// android/app/.../MainActivity.java@OverridepublicvoidinvokeDefaultOnBackPressed() {
    moveTaskToBack(true);
}
<!-- AndroidManifest.xml --><activity...android:launchMode="singleTop">

Link from where I get the solution

I hope I'm able to help guys with the same requirement.

Post a Comment for "Background A React-native Android App Using Back Button"