react-native-inappbrowser icon indicating copy to clipboard operation
react-native-inappbrowser copied to clipboard

Browser closing when app sent to background - Android

Open JustinJoyce88 opened this issue 4 years ago • 23 comments

As far as I can see, this is not intended. We have an app that opens up a form within the webview that requires the user to select a file or an image from the device. When selecting a file, the app gets sent to the background and when sent to the background, the browser closes. This is in the latest version, but if you downgrade to 3.3.4 and make sure that forceCloseOnRedirection is false, the webview should stay open. Please fix in the future.

JustinJoyce88 avatar Nov 13 '20 18:11 JustinJoyce88

Any pull request is welcome mate! 🙂

jdnichollsc avatar Nov 14 '20 08:11 jdnichollsc

@JustinJoyce88 would you mind specifying if you have some other configurations to make it work on the older version? I have 3.3.4 with forceCloseOnRedirection: false and the browser closes when returning to the app with {"type": "dismiss"}.

topisark avatar Nov 27 '20 10:11 topisark

It seems that ChromeTabsManagerActivity.java onDestroy gets called when returning to the app. I tried setting different launchModes to the activity but that doesn't seem to help. ☹️

topisark avatar Nov 27 '20 11:11 topisark

any luck on this issue? Getting on Android 9.

amit13091992 avatar Nov 27 '20 13:11 amit13091992

In your android-specific options for InAppBrowser.open add showInRecents: true. This should fix it.

InAppBrowser.open(url, {
     // Android Properties
    showInRecents: true,
    ........
})

lordkiz avatar Dec 03 '20 18:12 lordkiz

It looks similar to https://github.com/proyecto26/react-native-inappbrowser/issues/153.

Maybe https://github.com/proyecto26/react-native-inappbrowser/issues/153#issuecomment-727833604 helps?

oailloud avatar Dec 04 '20 12:12 oailloud

It looks similar to #153.

Maybe #153 (comment) helps?

That do work in some way but it only works when I re-open the app from the android appswitcher. If I open it through the app icon it closes the browser.

Auticcat avatar Dec 17 '20 12:12 Auticcat

@Auticcat Was you able to resolve the case where browser is closed on android when app is launched from android app launcher icon? I am stuck in same think and looking for the fix. Thanks.

harishchopra86 avatar Feb 08 '21 17:02 harishchopra86

@Auticcat Was you able to resolve the case where browser is closed on android when app is launched from android app launcher icon? I am stuck in same think and looking for the fix. Thanks.

Nope, just had to deal with it.

If anyone is using this package and has an otp step during authentication, you'll probably have cases where this happens.

Auticcat avatar Mar 02 '21 09:03 Auticcat

It looks like a normal behavior with Android, the Activities are destroyed when the launch icon is pressed again. Let me know if you find any option to change that default native behavior 👍

jdnichollsc avatar Mar 02 '21 14:03 jdnichollsc

It looks similar to #153. Maybe #153 (comment) helps?

That do work in some way but it only works when I re-open the app from the android appswitcher. If I open it through the app icon it closes the browser.

I have the same issue here using the latest lib version. 😞

Tried setting the below params as suggested in #153 and they don't help. 😿

   forceCloseOnRedirection: false,
   showInRecents: true,

RallyXiaoXiao avatar May 08 '21 00:05 RallyXiaoXiao

Any solution on above. when the app icon is clicked the browser window gets closed

Kiran0791 avatar May 28 '21 06:05 Kiran0791

It looks similar to #153. Maybe #153 (comment) helps?

That do work in some way but it only works when I re-open the app from the android appswitcher. If I open it through the app icon it closes the browser.

I have the same issue here using the latest lib version.

Tried setting the below params as suggested in #153 and they don't help.

   forceCloseOnRedirection: false,
   showInRecents: true,

This works for me! Thanks!

gabpaet avatar Jul 13 '21 06:07 gabpaet

If I open it through the app icon it closes the browser.

If your launch activity has launchMode as singleTask it's because of it.

I'm not expert in Android but after some searching in google I make up with follow solution (I hope it will help you). You need to create launch activity with standard launch mode and after activity created start you main activity if it is not started yet.

  1. Create your BaseApplication where you can keep started activities
public class BaseApplication extends Application {
    private ArrayList<Class> runningActivities = new ArrayList<>();

    public void addActivityToStack (Class cls) {
        if (!runningActivities.contains(cls)) runningActivities.add(cls);
    }

    public void removeActivityFromStack (Class cls) {
        if (runningActivities.contains(cls)) runningActivities.remove(cls);
    }

    public boolean isActivityInBackStack (Class cls) {
        return runningActivities.contains(cls);
    }
}
  1. create LaunchActivity and make it as launcher
public class LaunchActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BaseApplication application = (BaseApplication) getApplication();
        // check that MainActivity is not started yet
        if (!application.isActivityInBackStack(MainActivity.class)) {
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
        }
        finish();
    }
}

in AndroidManifest.xml move android.intent.action.MAIN to LaunchActivity

        <activity android:name=".LaunchActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
  1. in MainActivity
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(null);
    ((BaseApplication) getApplication()).addActivityToStack(this.getClass());
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
    ((BaseApplication) getApplication()).removeActivityFromStack(this.getClass());
  }

Augustach avatar Jul 13 '21 09:07 Augustach

If I open it through the app icon it closes the browser.

If your launch activity has launchMode as singleTask it's because of it.

I'm not expert in Android but after some searching in google I make up with follow solution (I hope it will help you). You need to create launch activity with standard launch mode and after activity created start you main activity if it is not started yet.

  1. Create your BaseApplication where you can keep started activities
public class BaseApplication extends Application {
    private ArrayList<Class> runningActivities = new ArrayList<>();

    public void addActivityToStack (Class cls) {
        if (!runningActivities.contains(cls)) runningActivities.add(cls);
    }

    public void removeActivityFromStack (Class cls) {
        if (runningActivities.contains(cls)) runningActivities.remove(cls);
    }

    public boolean isActivityInBackStack (Class cls) {
        return runningActivities.contains(cls);
    }
}
  1. create LaunchActivity and make it as launcher
public class LaunchActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BaseApplication application = (BaseApplication) getApplication();
        // check that MainActivity is not started yet
        if (!application.isActivityInBackStack(MainActivity.class)) {
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
        }
        finish();
    }
}

in AndroidManifest.xml move android.intent.action.MAIN to LaunchActivity

        <activity android:name=".LaunchActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
  1. in MainActivity
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(null);
    ((BaseApplication) getApplication()).addActivityToStack(this.getClass());
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
    ((BaseApplication) getApplication()).removeActivityFromStack(this.getClass());
  }

Hi @Augustach

I'm trying to implement your solution but I have trouble understanding how BaseApplication and MainApplication work together.

I have this error after my app immediately crashed :

AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{my.app/my.app.LaunchActivity}: java.lang.ClassCastException: my.app.MainApplication cannot be cast to my.app.BaseApplication

My androidManifest.xml is like this :

<application android:name=".MainApplication" android:label="@string/app_name" android:icon="@mipmap/ic_launcher" android:roundIcon="@mipmap/ic_launcher_round" android:allowBackup="true" android:theme="@style/AppTheme" android:usesCleartextTraffic="true">
    <meta-data android:name="expo.modules.updates.ENABLED" android:value="true"/>
    <meta-data android:name="expo.modules.updates.EXPO_SDK_VERSION" android:value="42.0.0"/>
    <meta-data android:name="expo.modules.updates.EXPO_UPDATES_CHECK_ON_LAUNCH" android:value="NEVER"/>
    <meta-data android:name="expo.modules.updates.EXPO_UPDATES_LAUNCH_WAIT_MS" android:value="5000"/>
    <meta-data android:name="expo.modules.updates.EXPO_UPDATE_URL" android:value="https://exp.host/@my.app"/>
    <activity android:name=".LaunchActivity">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
    <activity android:name=".MainActivity" android:label="@string/app_name" android:configChanges="keyboard|keyboardHidden|orientation|screenSize|uiMode" android:launchMode="standard" android:windowSoftInputMode="adjustResize" android:theme="@style/Theme.App.SplashScreen" android:screenOrientation="portrait">
      <intent-filter>
        <action android:name="android.intent.action.VIEW"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <category android:name="android.intent.category.BROWSABLE"/>
        <data android:scheme="my.app"/>
        <data android:scheme="my.app"/>
      </intent-filter>
    </activity>
    <activity android:name="com.facebook.react.devsupport.DevSettingsActivity"/>
    <activity android:name="com.theartofdev.edmodo.cropper.CropImageActivity" android:theme="@style/Base.Theme.AppCompat"/>
  </application>

Thanks for your help !

GautierT avatar Mar 11 '22 14:03 GautierT

Okay I found the solution to my problem.

I was creating a new BaseApplication class but I was supposed to use the existing one (MainApplication) and add

private ArrayList<Class> runningActivities = new ArrayList<>();

    public void addActivityToStack (Class cls) {
        if (!runningActivities.contains(cls)) runningActivities.add(cls);
    }

    public void removeActivityFromStack (Class cls) {
        if (runningActivities.contains(cls)) runningActivities.remove(cls);
    }

    public boolean isActivityInBackStack (Class cls) {
        return runningActivities.contains(cls);
    }

to it !

GautierT avatar Mar 11 '22 17:03 GautierT

@GautierT can you share your code? I got same issue with you

minhchienwikipedia avatar Jun 30 '22 07:06 minhchienwikipedia

Tried @Augustach's workaround in React native 0.68.2

BaseApplication application = (BaseApplication) getApplication();

can't cast Application to BaseApplication by some reason. So I used MainApplication. Looks it works even with deeplinks.

Bardiamist avatar Jul 04 '22 11:07 Bardiamist

anyone with full working example please?

ghmeec avatar Feb 25 '23 19:02 ghmeec

following this link helped me now its works just fine https://github.com/StardustCollective/stargazer-wallet-ext/pull/322/commits/a0cdb76c51b4a464106338572ebfb21c44009948

ghmeec avatar Feb 25 '23 21:02 ghmeec

In your android-specific options for InAppBrowser.open add showInRecents: true. This should fix it.

InAppBrowser.open(url, {
     // Android Properties
    showInRecents: true,
    ........
})

Thanks - showInRecents solved my issue

krini avatar Jan 09 '24 09:01 krini