22 May 2024 Leave a comment Tech-Help
In Android development, it’s common to encounter issues with activity restarts when the device orientation changes. This can lead to the activity being recreated and the onCreate
method being called again, which might not be desirable, especially if there is extensive initialization logic involved. Here, we will explore several strategies to handle this scenario effectively.
Using the Application Class
One effective approach is to move your initialization code to a custom Application class. The onCreate
method in the Application class is only called when the entire application is created, not on activity restarts due to orientation changes.
Kotlin Version
class MyApplicationClass: Application {
@Override
fun onCreate() {
super.onCreate()
// Put your application initialization code here
}
}
Java Version
public class MyApplicationClass extends Application {
@Override
public void onCreate() {
super.onCreate();
// Put your application initialization code here
}
}
Don’t forget to register your new Application class in the manifest:
<application
android:name="com.you.yourapp.MyApplicationClass"
Reacting to Configuration Changes
Another approach is to handle configuration changes manually by listening for events that cause restarts, such as orientation changes. This can be achieved by adding the android:configChanges
attribute to your activity in the manifest:
Then, override the onConfigurationChanged
method in your activity to handle the configuration changes:
Kotlin Version
@Override
fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
setContentView(R.layout.myLayout)
}
Java Version
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(R.layout.myLayout);
}
Checking savedInstanceState
in onCreate
If you need to differentiate between a fresh activity creation and a restart due to orientation change, you can check if savedInstanceState
is null:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
// Initial setup
}
}
Restricting Orientation
As a last resort, you can restrict your app to a single orientation to avoid activity restarts:
Using ViewModel
For a more modern approach, consider using the ViewModel class. ViewModel objects are automatically retained during configuration changes, making them ideal for storing UI-related data that needs to survive such changes.
For more information, refer to the official documentation.
Conclusion
Handling activity restarts on orientation changes can be managed in several ways, from using the Application class to handling configuration changes manually. Each method has its own benefits and use cases, so choose the one that best fits your application’s requirements.
For mobile developers looking to streamline their testing process, consider using Repeato. Repeato is a no-code test automation tool for iOS and Android that leverages computer vision and AI. It allows you to create, run, and maintain automated tests quickly and efficiently, freeing up more time to focus on developing great features. Learn more about how Repeato can enhance your testing workflow on our documentation page.