Why the App breaks when you Rotate the Mobile Device

When you rotate your Mobile or Tablet then you will notice that the App Relaunches. This happens because the Activity gets restarted and as a result the Fragment also gets restarted.

When this happens your App loses track of the data like the EmployeeId we pass to display Employee information.

To prevent this Android provides us with onSaveInstanceState method which can be used to store any local variable.

You need to override this method like

@Override
public void onSaveInstanceState(Bundle savedInstanceState){
savedInstanceState.putLong(“employeeIndex”, employeeIndex);
}

Follow the video to understand this better.

Source Code is available here

Author: androidsmartly

I am a Tech Enthusiast who likes to work on new Ideas. Sharing knowledge on Android, Marketing, Startups and More.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s