question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Android crashes nondeterministically when restoring from background

See original GitHub issue

Hi,

at least in release builds I’m getting crashes when restoring app from background, but not always. I replaced ReactActivity with ReactFragmentActivity.

react-native: 0.57.0

Fatal Exception: java.lang.RuntimeException: Unable to start activity ComponentInfo{com._redacted_.app/com._redacted_.app.MainActivity}: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.swmansion.rnscreens.Screen$ScreenFragment: calling Fragment constructor caused an exception
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2747)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2808)
       at android.app.ActivityThread.-wrap12(ActivityThread.java)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1541)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:165)
       at android.app.ActivityThread.main(ActivityThread.java:6365)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:883)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
       at android.support.v4.app.Fragment.instantiate(Fragment.java:386)
       at android.support.v4.app.FragmentContainer.instantiate(FragmentContainer.java:33)
       at android.support.v4.app.FragmentState.instantiate(FragmentState.java:79)
       at android.support.v4.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:3080)
       at android.support.v4.app.FragmentController.restoreAllState(FragmentController.java:152)
       at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:330)
       at com.facebook.react.ReactFragmentActivity.onCreate(ReactFragmentActivity.java:54)
       at com._redacted_.app.MainActivity.onCreate(MainActivity.java:19)
       at android.app.Activity.performCreate(Activity.java:6852)
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2700)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2808)
       at android.app.ActivityThread.-wrap12(ActivityThread.java)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1541)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:165)
       at android.app.ActivityThread.main(ActivityThread.java:6365)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:883)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
Caused by java.lang.reflect.InvocationTargetException
       at java.lang.reflect.Constructor.newInstance0(Constructor.java)
       at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
       at android.support.v4.app.Fragment.instantiate(Fragment.java:364)
       at android.support.v4.app.FragmentContainer.instantiate(FragmentContainer.java:33)
       at android.support.v4.app.FragmentState.instantiate(FragmentState.java:79)
       at android.support.v4.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:3080)
       at android.support.v4.app.FragmentController.restoreAllState(FragmentController.java:152)
       at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:330)
       at com.facebook.react.ReactFragmentActivity.onCreate(ReactFragmentActivity.java:54)
       at com._redacted_.app.MainActivity.onCreate(MainActivity.java:19)
       at android.app.Activity.performCreate(Activity.java:6852)
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2700)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2808)
       at android.app.ActivityThread.-wrap12(ActivityThread.java)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1541)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:165)
       at android.app.ActivityThread.main(ActivityThread.java:6365)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:883)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
Caused by java.lang.IllegalStateException: Screen fragments should never be restored
       at com.swmansion.rnscreens.Screen$ScreenFragment.<init>(Screen.java:19)
       at java.lang.reflect.Constructor.newInstance0(Constructor.java)
       at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
       at android.support.v4.app.Fragment.instantiate(Fragment.java:364)
       at android.support.v4.app.FragmentContainer.instantiate(FragmentContainer.java:33)
       at android.support.v4.app.FragmentState.instantiate(FragmentState.java:79)
       at android.support.v4.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:3080)
       at android.support.v4.app.FragmentController.restoreAllState(FragmentController.java:152)
       at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:330)
       at com.facebook.react.ReactFragmentActivity.onCreate(ReactFragmentActivity.java:54)
       at com._redacted_.app.MainActivity.onCreate(MainActivity.java:19)
       at android.app.Activity.performCreate(Activity.java:6852)
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2700)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2808)
       at android.app.ActivityThread.-wrap12(ActivityThread.java)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1541)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:165)
       at android.app.ActivityThread.main(ActivityThread.java:6365)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:883)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
Fatal Exception: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
       at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:2053)
       at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:2079)
       at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:678)
       at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:632)
       at com.swmansion.rnscreens.ScreenContainer.tryCommitTransaction(ScreenContainer.java:99)
       at com.swmansion.rnscreens.ScreenContainer.updateIfNeeded(ScreenContainer.java:173)
       at com.swmansion.rnscreens.ScreenContainer.access$000(ScreenContainer.java:21)
       at com.swmansion.rnscreens.ScreenContainer$1.doFrame(ScreenContainer.java:34)
       at com.facebook.react.modules.core.ReactChoreographer$ReactChoreographerDispatcher.doFrame(ReactChoreographer.java:134)
       at com.facebook.react.modules.core.ChoreographerCompat$FrameCallback$1.doFrame(ChoreographerCompat.java:105)
       at android.view.Choreographer$CallbackRecord.run(Choreographer.java:964)
       at android.view.Choreographer.doCallbacks(Choreographer.java:778)
       at android.view.Choreographer.doFrame(Choreographer.java:710)
       at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:952)
       at android.os.Handler.handleCallback(Handler.java:790)
       at android.os.Handler.dispatchMessage(Handler.java:99)
       at android.os.Looper.loop(Looper.java:172)
       at android.app.ActivityThread.main(ActivityThread.java:6590)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:12
  • Comments:72 (28 by maintainers)

github_iconTop GitHub Comments

105reactions
kmagieracommented, Sep 26, 2018

As a workaround for this issue I suggest adding the following code to the main activity class (the one that extends ReactFragmentActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(null);
}
14reactions
xfyrecommented, Nov 24, 2019

I was having this issue with RN 0.60.5 and react-native-screens 1.0.0-alpha22. Easily reproducible when developer option Don't keep activities is on.

Fixed the issue as @kmagiera suggested, by applying the following diff:

diff --git a/android/app/src/main/java/com/dcnetapp/MainActivity.java b/android/app/src/main/java/com/dcnetapp/MainActivity.java
index 5604708..043ae89 100644
--- a/android/app/src/main/java/com/dcnetapp/MainActivity.java
+++ b/android/app/src/main/java/com/dcnetapp/MainActivity.java
@@ -4,6 +4,7 @@ import com.facebook.react.ReactActivity;
 import com.facebook.react.ReactActivityDelegate;
 import com.facebook.react.ReactRootView;
 import com.swmansion.gesturehandler.react.RNGestureHandlerEnabledRootView;
+import android.os.Bundle;
 
 public class MainActivity extends ReactActivity {
     /**
@@ -24,4 +25,9 @@ public class MainActivity extends ReactActivity {
             }
         };
     }
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(null);
+    }
 }

The fix should be OK for now, but apparently it’s not a good long term solution. Is there any more proper way to solve for this?

Read more comments on GitHub >

github_iconTop Results From Across the Web

App crashes when restoring from background - Stack Overflow
The log shows a null pointer exception, I have a try/catch and the app is crashing in the catch, in a "Log.v("errorTag", e.getMessage());"...
Read more >
Crashes - Android Developers
Any app component, even components like broadcast receivers or content providers that are running in the background, can cause an app to crash....
Read more >
Why Do My Apps Keep Crashing on Android? - Avast
The most reliable way to fix crashing apps permanently is a full factory reset. This will return your Android phone to its factory...
Read more >
Why do my apps keep crashing on Android, How to fix it
Go to Settings -> Apps to uninstall unwanted apps and games. Advertisement. 9. Factory reset. When nothing works, you can try ...
Read more >
How to Fix Crashing Apps on an iPhone or Android?
I am a developer of iPhone/Android and my app keeps crashing on users phones… ... in the background or interferes with the behavior...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found