Skip to content

Browser closing when app sent to background - Android #213

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
JustinJoyce88 opened this issue Nov 13, 2020 · 25 comments
Open

Browser closing when app sent to background - Android #213

JustinJoyce88 opened this issue Nov 13, 2020 · 25 comments
Labels
discussion 🔥 Discussions about this plugin help wanted Extra attention is needed

Comments

@JustinJoyce88
Copy link

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 JustinJoyce88 changed the title Browser view closing when app sent to background - Android Webview closing when app sent to background - Android Nov 13, 2020
@jdnichollsc
Copy link
Member

Any pull request is welcome mate! 🙂

@topisark
Copy link

@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
Copy link

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. ☹️

@amit13091992
Copy link

any luck on this issue? Getting on Android 9.

@jdnichollsc jdnichollsc added the help wanted Extra attention is needed label Nov 30, 2020
@lordkiz
Copy link

lordkiz commented Dec 3, 2020

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

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

@oailloud
Copy link

oailloud commented Dec 4, 2020

It looks similar to #153.

Maybe #153 (comment) helps?

@Auticcat
Copy link

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.

@harishchopra86
Copy link

@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.

@jdnichollsc jdnichollsc changed the title Webview closing when app sent to background - Android Browser closing when app sent to background - Android Feb 8, 2021
@Auticcat
Copy link

Auticcat commented Mar 2, 2021

@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.

@jdnichollsc
Copy link
Member

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 👍

@RallyXiaoXiao
Copy link

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,

@Kiran0791
Copy link

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

@gabpaet
Copy link

gabpaet commented Jul 13, 2021

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!

@Augustach
Copy link

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());
  }

@GautierT
Copy link

GautierT commented Mar 11, 2022

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
Copy link

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 !

@minhchienwikipedia
Copy link

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

@Bardiamist
Copy link

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.

@jdnichollsc jdnichollsc added the discussion 🔥 Discussions about this plugin label Jul 29, 2022
@ghmeec
Copy link

ghmeec commented Feb 25, 2023

anyone with full working example please?

@ghmeec
Copy link

ghmeec commented Feb 25, 2023

following this link helped me now its works just fine
StardustCollective/stargazer-wallet-ext@a0cdb76

@krini
Copy link

krini commented Jan 9, 2024

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

@agueraXneuf
Copy link

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>
3. 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());
  }

Please how to do this with a react native kotlin project?

@Bardiamist
Copy link

Looks enough to add LaunchActivity.java

package com.valr.app;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class LaunchActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = new Intent(this, MainActivity.class);

        Bundle extras = getIntent().getExtras();

        // https://github.com/invertase/react-native-firebase/issues/3469#issuecomment-614990736
        if (extras != null) {
            intent.putExtras(extras);
        }

        startActivity(intent);

        finish();
    }

}

And use it

      <activity android:exported="true" 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|screenLayout|screenSize|smallestScreenSize|uiMode"
        android:launchMode="singleTask"
        android:windowSoftInputMode="adjustResize"
        android:exported="true"
        android:screenOrientation="portrait"
        tools:ignore="LockedOrientationActivity">

@njsm8
Copy link

njsm8 commented Oct 24, 2024

Tried the above but the app gets stuck at bootsplash when returning back to the app

@adenyx
Copy link

adenyx commented Mar 17, 2025

This comment resolved the same issue for me! Many thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion 🔥 Discussions about this plugin help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests