DEV Community

Abdulrasaq Durosomo
Abdulrasaq Durosomo

Posted on

HOW TO PREVENT ACTIVITY FROM RESTARTING WHEN PHONE IS ROTATED

I was working on android contact application where the add-contact form/context is a fragment on the main activity.

The features on the application were working fine until I tried it on rotation and found out that while on the add-contact-fragment, the main activity re-creates which causes the fragment to appear on the main activity(i.e activity_main.xml) as seen in the picture below

Alt Text

I started searching on the web for possible solutions and was trying out a whole lot of responses to related problem.

I finally stumbled over the succinct and helpful response and I am excited to write my first blog with a little explanation of how to solve this problem.

The details follows below:

Just handle the configuration on your manifest with the code below!

<activity
      android:configChanges="orientation|keyboardHidden|screenSize"
</activity>
Enter fullscreen mode Exit fullscreen mode

and thats all.

I do not want to bore you with too much details on this, the summary behind the change is that activity is being recreated on rotation, on keyboardhidden or screensize change and to prevent that use the code above in your manifest.xml file

However, if you are more curious you can dive into more details here

Thanks.

Top comments (0)