background a react-native android app using back button

前端 未结 2 1107
一整个雨季
一整个雨季 2020-12-06 08:10

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 s

相关标签:
2条回答
  • 2020-12-06 08:47

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

    public class MainActivity extends ReactActivity {
    
        @Override
        protected String getMainComponentName() {
            return "foo";
        }
    
        @Override
        public void invokeDefaultOnBackPressed() {
            // do not call super. invokeDefaultOnBackPressed() as it will close the app.  Instead lets just put it in the background.
            moveTaskToBack(true);
        }
    }
    
    0 讨论(0)
  • 2020-12-06 09:06

    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
    @Override
    public void invokeDefaultOnBackPressed() {
        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.

    0 讨论(0)
提交回复
热议问题