Activity restart on rotation Android

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for Activity restart on rotation Android

Activity Restart on Rotation in Android šŸ”„

šŸ“± Have you ever noticed that when you rotate your Android device, your app's activity gets restarted? This can be quite frustrating, especially if you have a lot of initial setup logic in the onCreate method. But don't worry, we've got you covered! In this blog post, we'll address this common issue and provide you with easy solutions to tackle it. Let's dive in! šŸŠā€ā™€ļø

Understanding the Problem šŸ¤”

When you rotate your Android device, the system recreates the activity. This is done to ensure that the UI adjusts accordingly to the new orientation. However, this can lead to the loss of initial setup logic performed in the onCreate method, which may not be what you want.

Solution #1: Moving Initial Setup Logic šŸ“¦

To prevent the loss of initial setup logic on activity rotation, you can move it to a separate function rather than having it all in the onCreate method. Let's call this function initialize for now. Here's an example:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initialize();
}

private void initialize() {
    // Perform your initial setup logic here
    // This code will be called both on activity creation and rotation
}

By doing this, the initialize function will be called both when the activity is created and when it is recreated on device rotation. This way, you can ensure that your initial setup is performed consistently.

Solution #2: Handling Configuration Changes šŸ”„

If you prefer not to recreate the activity on rotation, Android provides a way to handle configuration changes yourself. You can achieve this by adding the android:configChanges attribute to your activity in the manifest file. Here's an example:

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

By specifying the desired configuration changes, such as orientation and screenSize, Android will not recreate the activity when these changes occur. Instead, it will invoke the onConfigurationChanged method, which you can override to handle the necessary adjustments.

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    // Perform necessary UI adjustments here
}

Keep in mind that manually handling configuration changes can add complexity to your code, so use this approach judiciously.

Solution #3: Restricting Orientation 🚫

Another option is to restrict your app's orientation to a specific mode, such as portrait or landscape. By doing this, you can prevent the activity from being recreated on rotation altogether. You can specify the desired orientation in the manifest file or programmatically in your activity's onCreate method. Here are examples of both approaches:

Manifest file:

<activity android:name=".MainActivity"
    android:screenOrientation="portrait">
</activity>

Programmatic approach:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}

Conclusion and Your Next Steps šŸ

Now that you have learned about different solutions for handling activity restart on rotation in Android, it's time to implement the one that best fits your needs. Remember, you can move your initial setup logic to a separate function, handle configuration changes manually, or restrict the orientation to prevent recreation.

Choose the solution that works best for your app and give it a try! If you have any questions or other Android-related topics you'd like us to cover, leave a comment below. We'd love to hear from you! šŸ˜ŠšŸ’¬

Take Your Tech Career to the Next Level

Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

šŸ”„ šŸ’» šŸ†’ Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! šŸš€ Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings šŸ’„āœ‚ļø Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide šŸš€ So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? šŸ¤” Well, my