Aloha
Aloha

Reputation: 425

Android application stop unexpectedly when rotate

I am new to android application. My Android application works well on emulator but running on real android device (Softbank 003 SH), when I rotate the device from portrait to landscape, the application stop unexpectedly. Do you have any hint to solve this problem?

Upvotes: 2

Views: 3212

Answers (2)

Basbous
Basbous

Reputation: 3925

when rotate the device from landscape to portrait will re-create the activity so thread will stop and if any builder is running will cause a error so :

Start by adding the android:configChanges node to your Activity's manifest node

android:configChanges="keyboardHidden|orientation"

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

@Override
public void onConfigurationChanged(Configuration newConfig) {
  super.onConfigurationChanged(newConfig);
  setContentView(R.layout.myLayout);
}

Upvotes: 4

P Varga
P Varga

Reputation: 20229

You can simulate rotation on the emulator with CTRL+F12.

As for the "unexpected" stop, it is because when you rotate, the Activity is reloaded (onCreate is run again, etc), and you probably didn't follow the Android way of coding and some of your variables end up uninitialized! It is a very common error to assume Android works like Windows (has applications), but very generally, it works more like the iPhone or dynamic webpages (has semi-independent forms).

Check the LogCat for the error.

Upvotes: 1

Related Questions