---
title: "Activity restart on rotation Android"  
description: "Activity restart on rotation Android"  
author: "Samuel Fernandes"  
published: 2015-05-19  
updated: 2015-05-19  
canonical: https://www.mindstick.com/forum/23240/activity-restart-on-rotation-android  
category: "android"  
tags: ["android", "android activity"]  
reading_time: 2 minutes  

---

# Activity restart on rotation Android

In my [Android application](https://www.mindstick.com/forum/33405/how-to-get-the-build-version-number-of-your-android-application), when I rotate the [device](https://www.mindstick.com/blog/149/retriving-network-device-information-in-c-sharp) (slide out the [keyboard](https://www.mindstick.com/forum/23117/android-soft-keyboard-close-hide)) then my [Activity](https://www.mindstick.com/forum/12894/how-to-force-recreation-of-activity-fragment-on-restore) is restarted (onCreate is called). Now, this is probably how it's supposed to be, but I do a lot of initial [setting up](https://www.mindstick.com/articles/13112/setting-up-the-perfect-configuration-for-your-work-computer) in the onCreate [method](https://www.mindstick.com/forum/166/webservice-method), so I need either:\
Put all the initial setting up in another [function](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) so it's not all lost on device rotation orMake it so onCreate is not called again and the [layout](https://www.mindstick.com/articles/12853/android-layout-and-its-type) just adjusts orLimit the app to just portrait so that onCreate is not called.

## Replies

### Reply by Anonymous User

Depending on what you're doing in your initialization you could consider creating a new class that extends [Application](https://www.mindstick.com/articles/12824/calculator-application-in-android) and moving your initialization code into an overridden onCreate method within that class.\

```
public class MyApplicationClass extends Application {  @Override  public void onCreate() {    super.onCreate();    // TODO Put your application initialization code here.  }}
```

The onCreate in the application class is only called when the entire application is created, so the Activity restarts on orientation or keyboard visibility changes won't trigger it.\
It's good practice to expose the instance of this class as a singleton and exposing the application variables you're initializing using getters and setters.\
NOTE: You'll need to specify the name of your new Application class in the manifest for it to be registered and used:\

```
<application    android:name="com.you.yourapp.MyApplicationClass"
```

Reacting to Configuration Changes\
As a further alternative, you can have your application listen for events that would cause a restart – like orientation and keyboard visibility changes – and handle them within your Activity.\
Start by adding the [android](https://www.mindstick.com/articles/13046/10-reasons-why-i-prefer-android-vs-iphone-ios):configChanges node to your Activity's manifest node\

```
android:configChanges="keyboardHidden|orientation"
```

or for Android 3.2 (API level 13) and newer:\

```
android:configChanges="keyboardHidden|orientation|screenSize"
```

Then within the Activity override the onConfigurationChanged method and call setContentView to force the GUI layout to be re-done in the new orientation.\

```
@Overridepublic void onConfigurationChanged(Configuration newConfig) {  super.onConfigurationChanged(newConfig);  setContentView(R.layout.myLayout);}
```


---

Original Source: https://www.mindstick.com/forum/23240/activity-restart-on-rotation-android

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
