Skip to main content

Dealing with configuration changes

 How to deal with device configuration changes such as change to the screen rotation?


Because whenever there is change in device configuration the activity gets destroyed and re-created,which meant that local variables used by the activity were lost.

There are two options: we can either tell Android to bypass restarting the activity, or we can save its current state so that the activity can re-create itself in the same state.
1) Bypass re-creating the activity

We can tell the android not to restart the activity if there's been a configuration change.Usually it's not the best option.This is because when Android re-creates the activity, it makes sure it uses the right resources for the new configuration. If you bypass this, you may have to write a bunch of extra code to deal with the new configuration yourself.

We can add some line of code in activity element in AndroidManifest.xml
Example:
<activity
android:configChanges="orientation|screenSize"
>

Here we are mentioning two things one is orientation and screenSize.Because when the device rotate the devices changes both the orientation and the screen size.

Now if the android experience these type of orientation changes,it makes a call to the 'onConfigurationChanged(Configuration)' method instead of re-creating the activity.

2)Save the current state

the better way of dealing with configuration changes is saving the current state of the activity,then reinstate it in the onCreate() method.

To save the current state we need to override a method onSaveInstanceState().
This method gets called just before activity gets destroyed so it is advisable to save the value of local variables.

This method takes only one parameter i.e a Bundle.
Bundle is used to store multiple values in a single object.

This Bundle object is passed to the onCreate() method. So the onCreate() method will be able to pick the values stored in the Bundle.
Code for saving the value inside onSaveInstanceState():

@Override
public void onSaveInstanceState(Bundle savedInstanceState)
{
savedInstanceState.put*("key", value);
}

After storing the value we need to get these values in onCreate() method to save the state.

protected void onCreate(Bundle savedInstanceState)
{
savedInstanceState.get*("key");
}

In both method * represents the type of value we will be using like Int,String etc.

Comments

Post a Comment

Popular posts from this blog

Firebase FireStore

  Firestore is a subset of Firebase-Real-Time Database Firestore is a NOSQL Cloud Database which is extremely,customizable,efficient and scalable. Unlike SQL,we don't need to specify the column name and entity before putting the data in the table rather in NOSQL we don't need to predefined the structure of the database. In case of NOSQL, Database is called collection and Tables are called Documents. Documents allow us to add key-value pair which is basically the json format because json is easy to manipulate,query and very flexible. We can add documents inside the collection by specifing its name otherwise firebase we create an id for it. SetUp: 1) Inside Android Studio,go to tools select Firebase. 2) CLick on Log an Analytics event below the Analytics. 3) You will be redirected to login with your google account. 4) Log In into your account. 5) Go back to Studio you will that the Connected will be written with checked sign. 6) Now click on Add Analytics to your app.Now android ...

Android Basics

  The Android Runtime(Art): Java apps take too much memory and time to run inside the JVM especially running on low powered processors or low memory device.So in order to decrease these two factors Android have their own virtual machine to run the apps called the Android Runtime(Art). First .java code is converted to .class code. Then .class code is converted to .dex file. Finally the .dex file is converted .apk file. All .class, .dex, .apk are machine readable language. Android SDK: The Android Software Development Kit contains the libraries and tools you need to develop android apps. Some main things inside SDK: 1)SDK Platform: There's one for each version of Android. 2) SDK Tools: Tools for useful utilities like debugging and testing. 3) Sample Apps: To get information about how to use some APIs sample apps provide great help. 4)Documentation: Offline documentation. 5) Android Support: Extra APIs that aren't available in the standard platform. 6) Google Play Billing: It help...

Creating Chooser for Intents

  There's a way around you can create your own chooser that asks you to pick an activity without asking if you always want to use it. This task can be acheived using 'Intent.createChooser()'. Intent.createChooser() allows you to specify a title for the chooser dialog, and doesn’t give the user the option of selecting an activity to use by default. It also lets the user know if there are no matching activities by displaying a message. Syntax: Intent chosenIntent = Intent.createChooser(intent, "Send message..."); intent is the original Intent that we have made which describe the types of activity you want the chooser to display. "Send message..." here specifies the title of the activity chooser. The createChooser() method returns a brand-new Intent. What happens in the background: 1) The createChooser() method gets called. The method includes an intent that specifies the action and MIME type that’s required. 2)Android create a chooser for activities that ...