One option is using android:configChanges="orientation"
to tell android that you want to handle the configuration changes yourself instead of having it recreate the Activity. However, this is discouraged:
When a configuration change occurs at runtime, the activity is shut
down and restarted by default, but declaring a configuration with this
attribute will prevent the activity from being restarted. Instead, the
activity remains running and its onConfigurationChanged() method is
called. Note: Using this attribute should be avoided and used only as
a last resort. Please read Handling Runtime Changes for more
information about how to properly handle a restart due to a
configuration change.
( Source)
There is a different way to retain an object during a configuration change: The ViewModel can be used to define a wrapper class for your data. Each instance of the Activity seems to have its own set of ViewModels, accessible through
MyViewModel model = ViewModelProviders.of(this).get(MyViewModel.class);
and if you use LiveData
within it, you can subscribe to data changes as outlined in the documentation. Here is their example just in case the link dies at some point:
Architecture Components provides ViewModel helper class for the UI
controller that is responsible for preparing data for the UI.
ViewModel objects are automatically retained during configuration
changes so that data they hold is immediately available to the next
activity or fragment instance. For example, if you need to display a
list of users in your app, make sure to assign responsibility to
acquire and keep the list of users to a ViewModel, instead of an
activity or fragment, as illustrated by the following sample code:
public class MyViewModel extends ViewModel {
private MutableLiveData<List<User>> users;
public LiveData<List<User>> getUsers() {
if (users == null) {
users = new MutableLiveData<List<User>>();
loadUsers();
}
return users;
}
private void loadUsers() {
// Do an asynchronous operation to fetch users.
} } You can then access the list from an activity as follows:
public class MyActivity extends AppCompatActivity {
public void onCreate(Bundle savedInstanceState) {
// Create a ViewModel the first time the system calls an activity's onCreate() method.
// Re-created activities receive the same MyViewModel instance created by the first activity.
MyViewModel model = ViewModelProviders.of(this).get(MyViewModel.class);
model.getUsers().observe(this, users -> {
// update UI
});
} }
If the activity is re-created, it receives the same MyViewModel
instance that was created by the first activity. When the
owner activity is finished, the framework calls the ViewModel
objects's onCleared() method so that it can clean up resources.