Skip to content Skip to sidebar Skip to footer

My App Is Not Working Properly When Screen Is Rotated

I am creating an app on android studio. The app is working fine but whenever I rotate the screen, the layout is changing, the items on the screen are getting overlapped and sometim

Solution 1:

You need to set orientation in AndroidMenifest.xml.Use keyboardHidden attribute if you are getting any issue in keyboard.

Solution 2:

You can set you activity's screen orientation to portrait, by this your activity will never rotate, but if you want to support landscape version then you have to create a landscape layout as well and put the layout in layout-landscape folder, but you have to take care of your data also.

Choice is yours

Solution 3:

You can prevent your activity to recreate when orientation changes. Just add this in your activity tag in Manifest file.

<activityandroid:name=".YOUR_ACTIVITY_NAME"android:configChanges="keyboardHidden|orientation|screenSize" />

Solution 4:

Android Activites start from the beginning when you rotate a device. So your Activity Lifecycle methods like

onCreate(), onStart(), onResume()

will be called again. There are two possible solutions, the first one is by Rob here.

<activity
android:name=".ACTIVITY"android:configChanges="keyboardHidden|orientation|screenSize" />

This will handle the orientation changes itself.

Another solution is to create different layout types in different layout folders. The folders will be

layout-land, layout-sw600dp-land(for 7inch tablets) and layout-sw720dp-land(for 10inch tablets)

So when you rotate a device, the layouts in this folders will be automatically inflated by the system. There will be cases like when you want to preserve the current state of the activity, for this refer to this link

Solution 5:

No, preventing the orientation to be landscape is not a good solution. Many tablets are landscape-only, and your app won't display correctly on those.

The correct thing to do is to save your state in onSaveInstanceState() and then read it in onCreate() when the activity is re-created (the savedInstanceState variable will not be null). For this to work, the data that you are saving must be Serializable or Parcelable.

If you want to save non-serializable data or you have something running in the background, you can use a Fragment with setRetainInstance(true); and no layout. See this link.

The view will be created again, yes, but you will set the saved values so that it will look as it was before the rotation.

Post a Comment for "My App Is Not Working Properly When Screen Is Rotated"