my app has a dialog and two activities, landscape and portrait.
when I'm on one of the activities and the screen is rotated while the dialog is open the app crashes.
What should I 'handle' for that not to happen?
This is the logcat output: (The space indicates where things start to go south)
11-15 15:56:25.322: D/AndroidRuntime(956): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
11-15 15:56:25.322: D/AndroidRuntime(956): CheckJNI is ON
11-15 15:56:26.342: W/ThrottleService(77): unable to find stats for iface rmnet0
11-15 15:56:26.382: D/AndroidRuntime(956): Calling main entry com.android.commands.pm.Pm
11-15 15:56:26.442: D/AndroidRuntime(956): Shutting down VM
11-15 15:56:26.452: D/dalvikvm(956): GC_CONCURRENT freed 101K, 78% free 463K/2048K, paused 1ms+1ms
11-15 15:56:26.462: D/dalvikvm(956): Debugger has detached; object registry had 1 entries
11-15 15:56:26.472: I/AndroidRuntime(956): NOTE: attach of thread 'Binder Thread #3' failed
11-15 15:56:27.112: D/AndroidRuntime(969): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
11-15 15:56:27.112: D/AndroidRuntime(969): CheckJNI is ON
11-15 15:56:28.002: D/AndroidRuntime(969): Calling main entry com.android.commands.am.Am
11-15 15:56:28.032: I/ActivityManager(77): START {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.soft.test/.myActivity} from pid 969
11-15 15:56:28.032: W/WindowManager(77): Failure taking screenshot for (130x178) to layer 21005
11-15 15:56:28.092: D/AndroidRuntime(969): Shutting down VM
11-15 15:56:28.142: D/dalvikvm(980): Not late-enabling CheckJNI (already on)
11-15 15:56:28.163: D/dalvikvm(969): GC_CONCURRENT freed 102K, 77% free 485K/2048K, paused 1ms+1ms
11-15 15:56:28.163: D/dalvikvm(969): Debugger has detached; object registry had 1 entries
11-15 15:56:28.172: I/AndroidRuntime(969): NOTE: attach of thread 'Binder Thread #3' failed
11-15 15:56:28.252: I/ActivityManager(77): Start proc com.soft.test for activity com.soft.test/.myActivity: pid=980 uid=10046 gids={3003}
11-15 15:56:28.362: I/dalvikvm(980): Turning on JNI app bug workarounds for target SDK version 8...
11-15 15:56:28.452: W/NetworkManagementSocketTagger(77): setKernelCountSet(10046, 1) failed with errno -2
11-15 15:56:29.652: D/dalvikvm(980): GC_CONCURRENT freed 222K, 5% free 6723K/7047K, paused 5ms+16ms
11-15 15:56:29.873: V/PhoneStatusBar(141): setLightsOn(true)
11-15 15:56:29.953: D/gralloc_goldfish(980): Emulator without GPU emulation detected.
11-15 15:56:30.303: I/ActivityManager(77): Displayed com.soft.test/.myActivity: +2s234ms
11-15 15:56:38.073: W/ActivityManager(77): Launch timeout has expired, giving up wake lock!
11-15 15:56:38.510: W/ActivityManager(77): Activity idle timeout for ActivityRecord{4119aad0 com.soft.test/.myActivity}
11-15 15:56:38.563: W/NetworkManagementSocketTagger(77): setKernelCountSet(10009, 0) failed with errno -2
11-15 15:56:54.973: D/dalvikvm(980): GC_CONCURRENT freed 93K, 3% free 7095K/7303K, paused 7ms+27ms
11-15 15:57:00.052: I/InputReader(77): Reconfiguring input devices. changes=0x00000004
11-15 15:57:00.052: I/InputReader(77): Device reconfigured: id=0, name='qwerty2', surface size is now 320x480, mode is 1
11-15 15:57:00.052: W/SurfaceFlinger(35): createScreenshotSurface failed (Function not implemented)
11-15 15:57:00.062: I/ActivityManager(77): Config changed: {1.0 310mcc260mnc en_US layoutdir=0 sw320dp w320dp h407dp smll port finger -keyb/v/h tball/v s.14}
11-15 15:57:01.072: D/dalvikvm(980): newInstance failed: no <init>()
11-15 15:57:01.072: D/AndroidRuntime(980): Shutting down VM
11-15 15:57:01.082: W/dalvikvm(980): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
11-15 15:57:01.122: E/AndroidRuntime(980): FATAL EXCEPTION: main
11-15 15:57:01.122: E/AndroidRuntime(980): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.soft.test/com.soft.test.myActivity}: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.soft.test.HistoryDialogFragment: make sure class name exists, is public, and has an empty constructor that is public
11-15 15:57:01.122: E/AndroidRuntime(980): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
11-15 15:57:01.122: E/AndroidRuntime(980): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
11-15 15:57:01.122: E/AndroidRuntime(980): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3351)
11-15 15:57:01.122: E/AndroidRuntime(980): at android.app.ActivityThread.access$700(ActivityThread.java:123)
11-15 15:57:01.122: E/AndroidRuntime(980): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1151)
11-15 15:57:01.122: E/AndroidRuntime(980): at android.os.Handler.dispatchMessage(Handler.java:99)
11-15 15:57:01.122: E/AndroidRuntime(980): at android.os.Looper.loop(Looper.java:137)
11-15 15:57:01.122: E/AndroidRuntime(980): at android.app.ActivityThread.main(ActivityThread.java:4424)
11-15 15:57:01.122: E/AndroidRuntime(980): at java.lang.reflect.Method.invokeNative(Native Method)
11-15 15:57:01.122: E/AndroidRuntime(980): at java.lang.reflect.Method.invoke(Method.java:511)
11-15 15:57:01.122: E/AndroidRuntime(980): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-15 15:57:01.122: E/AndroidRuntime(980): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-15 15:57:01.122: E/AndroidRuntime(980): at dalvik.system.NativeStart.main(Native Method)
11-15 15:57:01.122: E/AndroidRuntime(980): Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.soft.test.HistoryDialogFragment: make sure class name exists, is public, and has an empty constructor that is public
11-15 15:57:01.122: E/AndroidRuntime(980): at android.support.v4.app.Fragment.instantiate(Fragment.java:399)
11-15 15:57:01.122: E/AndroidRuntime(980): at android.support.v4.app.FragmentState.instantiate(Fragment.java:97)
11-15 15:57:01.122: E/AndroidRuntime(980): at android.support.v4.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1760)
11-15 15:57:01.122: E/AndroidRuntime(980): at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:200)
11-15 15:57:01.122: E/AndroidRuntime(980): at com.soft.test.myActivity.onCreate(myActivity.java:66)
11-15 15:57:01.122: E/AndroidRuntime(980): at android.app.Activity.performCreate(Activity.java:4465)
11-15 15:57:01.122: E/AndroidRuntime(980): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
11-15 15:57:01.122: E/AndroidRuntime(980): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
11-15 15:57:01.122: E/AndroidRuntime(980): ... 12 more
11-15 15:57:01.122: E/AndroidRuntime(980): Caused by: java.lang.InstantiationException: can't instantiate class com.soft.test.HistoryDialogFragment; no empty constructor
11-15 15:57:01.122: E/AndroidRuntime(980): at java.lang.Class.newInstanceImpl(Native Method)
11-15 15:57:01.122: E/AndroidRuntime(980): at java.lang.Class.newInstance(Class.java:1319)
11-15 15:57:01.122: E/AndroidRuntime(980): at android.support.v4.app.Fragment.instantiate(Fragment.java:388)
11-15 15:57:01.122: E/AndroidRuntime(980): ... 19 more
11-15 15:57:01.192: W/ActivityManager(77): Force finishing activity com.soft.test/.myActivity
11-15 15:57:01.312: I/ARMAssembler(35): generated scanline__00000077:03545404_00008500_00000000 [ 29 ipp] (43 ins) at [0x41357cd8:0x41357d84] in 5238733 ns
11-15 15:57:01.322: W/WindowManager(77): Failure taking screenshot for (120x162) to layer 21010
11-15 15:57:01.832: W/ActivityManager(77): Activity pause timeout for ActivityRecord{4119aad0 com.soft.test/.myActivity}
11-15 15:57:01.932: W/NetworkManagementSocketTagger(77): setKernelCountSet(10009, 1) failed with errno -2
11-15 15:57:02.152: D/dalvikvm(141): GC_CONCURRENT freed 376K, 21% free 7320K/9159K, paused 4ms+121ms
11-15 15:57:02.282: D/dalvikvm(183): GC_EXPLICIT freed 201K, 8% free 7501K/8071K, paused 22ms+14ms
11-15 15:57:02.292: E/StrictMode(183): class com.android.launcher2.Launcher; instances=2; limit=1
11-15 15:57:02.292: E/StrictMode(183): android.os.StrictMode$InstanceCountViolation: class com.android.launcher2.Launcher; instances=2; limit=1
11-15 15:57:02.292: E/StrictMode(183): at android.os.StrictMode.setClassInstanceLimit(StrictMode.java:1)
11-15 15:57:03.432: W/NetworkManagementSocketTagger(77): setKernelCountSet(10046, 0) failed with errno -2
11-15 15:57:03.792: D/dalvikvm(209): GC_CONCURRENT freed 155K, 4% free 6694K/6919K, paused 4ms+3ms
11-15 15:57:04.933: I/Process(980): Sending signal. PID: 980 SIG: 9
11-15 15:57:05.273: I/ActivityManager(77): Process com.soft.test (pid 980) has died.
11-15 15:57:05.273: I/WindowManager(77): WIN DEATH: Window{4122bc60 com.soft.test/com.soft.test.myActivity paused=false}
11-15 15:57:05.513: D/dalvikvm(77): GC_EXPLICIT freed 237K, 11% free 8967K/10055K, paused 6ms+11ms
11-15 15:57:13.477: W/ActivityManager(77): Activity destroy timeout for ActivityRecord{4119aad0 com.soft.test/.myActivity}
There are apps that don't respect your phone's default orientation option, which means these apps don't rotate even if you've enabled the auto-rotate option. Luckily, most of these apps offer an option within them to rotate the screen.
The answer is that you cannot globally set orientation. Any app can choose to handle orientation itself, and if it does so then it can choose to ignore changes to the device's orientation, or to change orientation based on its own criteria.
Apps on Android can crash because of low storage space, too many apps running simultaneously, a weak internet connection, or not having the proper app updates installed.
The easiest way to fix an app that keeps crashing on your Android smartphone is to simply force stop it and open it again. To do this, go to Settings -> Apps and select the app that keeps crashing. Tap on the app's name and then tap on 'Force stop'. Now try opening the app again and see if it works well.
When the phone rotates an Activity always gets destroyed and re-created. This means it will also try to re-create the Fragment. Since your Fragment lacks an empty constructor it cannot be recreated, thus the error: "com.soft.test.HistoryDialogFragment; no empty constructor
".
You can also use "setRetainInstance(true)
" in your Fragments onCreate() to make it so it is not killed but only removed (detached from the activity) temporarily.
Here's an example: http://android-er.blogspot.se/2012/08/retain-fragment-instance-across.html
Here you can read more: https://stackoverflow.com/a/11318942/1068167
If you're okay with the DialogFragment
being closed on a screen rotation, this is another possible solution. Keep an instance variable current_dialog
that keeps track of the DialogFragment
that is currently open, then add this code to your onPause()
:
if (current_dialog != null)
current_dialog.dismiss();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With