react-native-inappbrowser
react-native-inappbrowser copied to clipboard
Browser closing when app sent to background - Android
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.
Any pull request is welcome mate! 🙂
@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"}
.
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. ☹️
any luck on this issue? Getting on Android 9.
In your android-specific options for InAppBrowser.open add showInRecents: true
. This should fix it.
InAppBrowser.open(url, {
// Android Properties
showInRecents: true,
........
})
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?
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 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.
@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.
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 👍
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,
Any solution on above. when the app icon is clicked the browser window gets closed
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!
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.
- 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);
}
}
- 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>
- 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());
}
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.
- 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); } }
- 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>
- 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 !
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 can you share your code? I got same issue with you
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.
anyone with full working example please?
following this link helped me now its works just fine https://github.com/StardustCollective/stargazer-wallet-ext/pull/322/commits/a0cdb76c51b4a464106338572ebfb21c44009948
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