3

how do you keep the activity from restarting when the screen rotates or when then user slides the keyboard on the phone? Is this possible? Is there a work around or something? All relevant answers are appreciated.

2 Answers 2

7

You can do this by declaring a specific attribute in your activity element in your manifest.xml. The element in question is called android:configChanges, and you need to register the string value of orientation.

<activity android:name=".MyActivity"
      android:configChanges="orientation"
      android:label="@string/app_name">

From the documentation:

Now when one of these configurations change, MyActivity is not restarted. Instead, the Activity receives a call to onConfigurationChanged(). This method is passed a Configuration object that specifies the new device configuration. By reading fields in the Configuration, you can determine the new configuration and make appropriate changes by updating the resources used in your interface. At the time this method is called, your Activity's Resources object is updated to return resources based on the new configuration, so you can easily reset elements of your UI without the system restarting your Activity

So doing this will cause your Activity to not restart, and will also callback to onConfigurationChanged() so that you can handle the change yourself.

Sign up to request clarification or add additional context in comments.

3 Comments

Can you give me some sample code? I cant seem to get it to work.
What is it that you cannot seem to get to work? Could you provide an update to your question by editing it? It would be helpful if you would mention what you have tried, and what specifically isn't working.
In addition of "orientation", per your question you might also want to add related keyboard config changes to handle keyboard action, so the value that you might want to put is "orientation|keyboard|keyboardHidden". As Nicholas said, you probably want/need to handle onConfigurationChanged() yourself
0

If you read the documentation here, you will see that you can specify the following in your manifest:

<activity ...
    android:configChanges="orientation">

Once you have this you can implement the onConfigurationChanged() method to receive notifications about orientation change, or just use the base class's implementation.

1 Comment

@Arvin The two answers are 4 minutes apart. It's unlikely that this answer was a "copy", just an equivalent answer that was posted concurrently. It happens all the time and isn't a big deal. People generally upvote the earlier answer more unless the later answer actually adds something useful. Your comment sounds like sour grapes for having your own answer downvoted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.