3

I am new to android development. I have developed one app, now the problem is that whenever I rotate screen, app starts activity again and then its crashes application. I found some solution on google to use android:configChanges="orientation" . It works fine but when I rotate screen, its screen size does not change. How to solve this problem ?

Logact :

05-28 14:32:52.038: E/AndroidRuntime(276): FATAL EXCEPTION: main 05-28 14:32:52.038: E/AndroidRuntime(276): java.lang.IllegalArgumentException: View not attached to window manager 05-28 14:32:52.038: E/AndroidRuntime(276): at android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:355) 05-28 14:32:52.038: E/AndroidRuntime(276): at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:200) 05-28 14:32:52.038: E/AndroidRuntime(276): at android.view.Window$LocalWindowManager.removeView(Window.java:432) 05-28 14:32:52.038: E/AndroidRuntime(276): at android.app.Dialog.dismissDialog(Dialog.java:278) 05-28 14:32:52.038: E/AndroidRuntime(276): at android.app.Dialog.access$000(Dialog.java:71) 05-28 14:32:52.038: E/AndroidRuntime(276): at android.app.Dialog$1.run(Dialog.java:111) 05-28 14:32:52.038: E/AndroidRuntime(276): at android.app.Dialog.dismiss(Dialog.java:268) 05-28 14:32:52.038: E/AndroidRuntime(276): at android.app.Dialog.cancel(Dialog.java:951) 05-28 14:32:52.038: E/AndroidRuntime(276): at com.MobileWeb.mytestbuddy.PremiumMain$TheTask.onPostExecute(PremiumMain.java:373) 05-28 14:32:52.038: E/AndroidRuntime(276): at com.MobileWeb.mytestbuddy.PremiumMain$TheTask.onPostExecute(PremiumMain.java:1) 05-28 14:32:52.038: E/AndroidRuntime(276): at android.os.AsyncTask.finish(AsyncTask.java:417) 05-28 14:32:52.038: E/AndroidRuntime(276): at android.os.AsyncTask.access$300(AsyncTask.java:127) 05-28 14:32:52.038: E/AndroidRuntime(276): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429) 05-28 14:32:52.038: E/AndroidRuntime(276): at android.os.Handler.dispatchMessage(Handler.java:99) 05-28 14:32:52.038: E/AndroidRuntime(276): at android.os.Looper.loop(Looper.java:123) 05-28 14:32:52.038: E/AndroidRuntime(276): at android.app.ActivityThread.main(ActivityThread.java:4627) 05-28 14:32:52.038: E/AndroidRuntime(276): at java.lang.reflect.Method.invokeNative(Native Method) 05-28 14:32:52.038: E/AndroidRuntime(276): at java.lang.reflect.Method.invoke(Method.java:521) 05-28 14:32:52.038: E/AndroidRuntime(276): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 05-28 14:32:52.038: E/AndroidRuntime(276): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 05-28 14:32:52.038: E/AndroidRuntime(276): at dalvik.system.NativeStart.main(Native Method)

Jeeten Parmar
  • 195
  • 2
  • 2
  • 10
  • 2
    you have to remove bugs from your app ... – Selvin May 28 '13 at 09:00
  • But it is working fine. What should i check ? – Jeeten Parmar May 28 '13 at 09:01
  • this cant be answered without more details and some source code – danijoo May 28 '13 at 09:02
  • java.lang.IllegalArgumentException: View not attached to window manager – Jeeten Parmar May 28 '13 at 09:03
  • It will be a lot better if you can post your whole logcat – Mehul Joisar May 28 '13 at 09:06
  • 4
    I don't know why people are so much interested to vote down ??? You can develop well that does not mean you do like this with beginners. – Jeeten Parmar May 28 '13 at 09:22
  • @Jeeten, have a look at the google docs on handling runtime changes. It will explain nicely what happens when you change orientation and how to handle this. The solutions provided here are not great and the link at the top is a poor excuse to close this thread. Please see http://developer.android.com/guide/topics/resources/runtime-changes.html – Chris Aug 19 '14 at 14:26

2 Answers2

41

You can avoid activity recreation by adding following to your application's manifest file.

android:configChanges="orientation|keyboardHidden|screenSize"

As follows

      <activity
        android:name=".your activity"
        android:configChanges="orientation|keyboardHidden|screenSize"
        android:label="@string/app_name" >
    </activity>
Dinesh Anuruddha
  • 7,137
  • 6
  • 32
  • 45
  • but when i rotate screen from normal to portrait, its screen size does not change. what to do for this ??? – Jeeten Parmar May 28 '13 at 09:17
  • did you add above line to activity tag in your manifest? – Dinesh Anuruddha May 28 '13 at 09:18
  • wait, let me see my design part. I think it will solved my prob. Thank you. – Jeeten Parmar May 28 '13 at 09:21
  • are you testing with actual device or emulator? – Dinesh Anuruddha May 28 '13 at 09:22
  • 1
    I don't know why people are so much interested to vote down ??? You can develop well that does not mean you do like this with beginners. – Jeeten Parmar May 28 '13 at 09:22
  • im testing with emulator. – Jeeten Parmar May 28 '13 at 09:23
  • add above line to your manifest and try to test with actual device or ICS emulator.it will work for sure. – Dinesh Anuruddha May 28 '13 at 09:27
  • hey, when i rotate screen from landscape to portrait and portrait to landscape, its design does not change. – Jeeten Parmar May 28 '13 at 11:50
  • 1
    This solution worked for me. The layout also changed correctly as the screen was rotated as well. I know that this is an old answer, but is there a particular tradeoff when you put this in the manifest? Why would you not want to do this every time for each activity? – idungotnosn Dec 08 '14 at 16:35
  • @idungotnosn As shown in [documentation](https://developer.android.com/guide/topics/manifest/activity-element.html#config): **Using this attribute should be avoided and used only as a last resort. Please read [Handling Runtime Changes](https://developer.android.com/guide/topics/resources/runtime-changes.html) for more information about how to properly handle a restart due to a configuration change.** – afranques Jun 16 '16 at 09:48
  • Also, there's a good post talking about it here: http://stackoverflow.com/questions/7818717/why-not-use-always-androidconfigchanges-keyboardhiddenorientation – afranques Jun 16 '16 at 09:54
  • Worked for me! I was having freezes on many phones after calling setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE), which made it hard to understand why some phones worked and some not. Using configChanges in the manifest as mentioned in this comment solved it – reavcn Mar 05 '19 at 10:57
-2

Your question is so vague. All I can say is you need to check your log cat and have a read and see why your application crashes.

I would recommend a quick read on android:configChanges from here. Hope this helps you.

  • error is on dialog. code is : if (dialog != null) { if (dialog.isShowing()) { dialog.cancel(); } } – Jeeten Parmar May 28 '13 at 09:12
  • then why it is working when using oriention ? – Jeeten Parmar May 28 '13 at 09:12
  • I don't know why people are so much interested to vote down ??? You can develop well that does not mean you do like this with beginners. – Jeeten Parmar May 28 '13 at 09:23
  • Ignore them. =) I think "kaluwila" has given you an answer to what you should do. The link I posted tells you this: **"Note: If your application targets API level 13 or higher (as declared by the minSdkVersion and targetSdkVersion attributes), then you should also declare the "screenSize" configuration, because it also changes when a device switches between portrait and landscape orientations."** – Mazin Al-Bahrani May 28 '13 at 09:26
  • yes, mazin Al-Bahrni. It is working fine in actual device. then why it gives prob in emulator ? – Jeeten Parmar May 28 '13 at 09:29
  • I always had an issue with emulators since I couldn't really use the exact RAM and internal memory storage size. Also, the emulator may not work as needed due to inefficient resources due to Windows. (I work with Windows and always have that issue) I prefer using a real device since that way you won't deal with emulator issues at all. – Mazin Al-Bahrani May 28 '13 at 09:32
  • yeah, rite. But i am new to android so i think it will take some time for me to understand everything. – Jeeten Parmar May 28 '13 at 09:37