Chris
Chris

Reputation: 683

My first Android app crashes when rotated

My app crashes whenever I rotate the Nexus from portrait to landscape or landscape to portrait. It produces the exception report below, which I do not understand.

I basically did what it said, and found my main activity onDestroy function and added super.onDestroy, which made the problem go away.

However, why did this happen? Why is onDestroy coming into play just because I rotated my tablet?

02-18 00:43:28.504  28259-28259/com.mpr.myfirstapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.mpr.myfirstapp, PID: 28259
android.util.SuperNotCalledException: Activity {com.mpr.myfirstapp/com.mpr.myfirstapp.MainActivity} did not call through to super.onDestroy()
at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3660)
at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3689)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3889)
at android.app.ActivityThread.access$900(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1284)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5223)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

Upvotes: 3

Views: 18560

Answers (4)

Lucifer
Lucifer

Reputation: 29632

By looking your logs I assume you have onDestroy() method written as following,

@Override
protected void onDestroy() {
}

Actually you forgot to call super.onDestroy(); inside the onDestroy() method, so actually your onDestroy() method should looks like below,

@Override
protected void onDestroy() {
    super.onDestroy();               // Add this line
}

This should solve your problem.

Upvotes: 2

user1841702
user1841702

Reputation: 2763

The answer to this question can be found here:

http://developer.android.com/guide/topics/resources/runtime-changes.html

What you are experiencing is essentially the default behaviour of an Android activity. When a change in orientation occurs, the resources tied with the Activity are subject to change and need to be managed. These kind of situations are usually handled with the onSaveInsatanceState() and onRestoreInstanceState() methods and their usage is highlighted in the documentation.

Upvotes: 6

Anggrayudi H
Anggrayudi H

Reputation: 15155

Open your AndroidManifest.xml and add this attribute to your activity:

<activity android:name=".YourActivityName" android:configChanges="orientation|screenSize|keyboardHidden" />

The activity may recreated when you rotate the screen without add orientation value.

Upvotes: 26

Andrew G
Andrew G

Reputation: 2614

The Activity is destroyed prior to rotation. It is then created again in the new orientation.

Derived classes must call through to the super class's implementation of this method. If they do not, an exception will be thrown.

It's an implementation detail. It is thoroughly documented.

protected void onDestroy ()

Added in API level 1 Perform any final cleanup before an activity is destroyed. This can happen either because the activity is finishing (someone called finish() on it, or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the isFinishing() method.

Note: do not count on this method being called as a place for saving data! For example, if an activity is editing data in a content provider, those edits should be committed in either onPause() or onSaveInstanceState(Bundle), not here. This method is usually implemented to free resources like threads that are associated with an activity, so that a destroyed activity does not leave such things around while the rest of its application is still running. There are situations where the system will simply kill the activity's hosting process without calling this method (or any others) in it, so it should not be used to do things that are intended to remain around after the process goes away.

Derived classes must call through to the super class's implementation of this method. If they do not, an exception will be thrown.

Upvotes: 4

Related Questions