Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onDestroy gets called each time the screen goes on

Tags:

My application gets killed each time that it comes back from the screen-off-state. I fetch all the information that my app does, but I can't find out why it calls onDestroy. It's the first time I'm seeing this behavior in my applications.

My main activity extends tabActivity because it contains a tabhost. I've read that it has to extend it or it will FC. I'm not sure if my issue is related to this?! Oh and it implements Observer but this should be no problem.

Here are the logs:

07-21 09:57:53.247: VERBOSE/###(13180): onResume 07-21 09:57:53.267: VERBOSE/###(13180): onPause 07-21 09:57:59.967: VERBOSE/###(13180): onResume 07-21 09:58:00.597: VERBOSE/###(13180): onPause 07-21 09:58:00.597: VERBOSE/###(13180): onDestroy 07-21 09:58:00.637: VERBOSE/###(13180): onCreate 

The crazy thing is that it calls the onDestroy the most times after the screen goes on again, and sometimes it has enough time to do this before the screen goes off. But after it goes on again it does the same again...

I hope that someone has a tip for me or any information on how to resolve this issue.

I'm not sure if this is important, but I use the android 2.1-update1 sdk for my application.


EDIT:

The application gets tested on a real Android Device.

Here is some basic code with all unnecessary lines and information removed:

package; imports;  public class WebLabActivity extends TabActivity implements Observer{  #declerations  /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     Log.v("###", "onCreate");     setContentView(R.layout.main);     # initialize some basic things }  @Override public void onResume() {     super.onResume();     Log.v("###", "onResume"); }  @Override public void onDestroy() {     super.onDestroy();     Log.v("###", "onDestroy"); }  @Override public void onRestart() {     Log.v("###", "onRestart");     super.onRestart(); }  @Override public void onPause() {     Log.v("###", "onPause");     super.onPause(); }  @Override public void onConfigurationChanged(Configuration newConfig) {     Log.v("###", "onConfigurationChanged");     super.onConfigurationChanged(newConfig); }  @Override public void update(Observable observable, Object data) {     Log.v("###", "notifyManager.getWho() + " made an Update"); }       private void initializeSidebarTabhost() {     TabSpec 1 = tabHost.newTabSpec("1");         TabSpec 2 = tabHost.newTabSpec("2");     TabSpec 3 = tabHost.newTabSpec("3");     TabSpec 4 = tabHost.newTabSpec("4");       1.setIndicator("###");     2.setIndicator("###");     3.setIndicator("###");     4.setIndicator("###");      addIntents      tabHost.addTab(1); //0     tabHost.addTab(2); //1     tabHost.addTab(3); //2     tabHost.addTab(4); //3      tabHost.getTabWidget().setCurrentTab(2); } } 

EDIT2:

Ok, I've tested my application without initializing anything, then with only extending activity, or without implementing observer, but my changes had no effect. Every time I set my phone to sleep, then wake it up, onDestroy() get's called?!


EDIT3:

Ok, I found out something interesting.

First here's my AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"       package="com.tundem.###"       android:versionCode="1"       android:versionName="1.0">     <uses-sdk android:minSdkVersion="7" />      <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />     <uses-permission android:name="android.permission.INTERNET" />     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />      <application android:icon="@drawable/icon" android:label="@string/app_name">         <activity android:name=".###" android:label="@string/app_name" android:screenOrientation="landscape" android:theme="@android:style/Theme.Light.NoTitleBar">             <intent-filter>                 <action android:name="android.intent.action.MAIN" />                 <category android:name="android.intent.category.LAUNCHER" />             </intent-filter>         </activity>     </application> </manifest> 

As soon as I remove the screenOrientation="landscape", the application won't be destroyed each time that I wake up my device. I tried it more than 10 times but no more calls to onDestroy()

So I think that I will have to set this in code?! Any tips or pieces of code?

like image 845
mikepenz Avatar asked Jul 21 '11 08:07

mikepenz


People also ask

What happens when onDestroy is called?

If onDestroy() is called as the result of a configuration change, the system immediately creates a new activity instance and then calls onCreate() on that new instance in the new configuration. The onDestroy() callback should release all resources that have not yet been released by earlier callbacks such as onStop() .

Is onDestroy guaranteed to be called?

Android Activity onDestroy() is not always called and if called only part of the code is executed. Bookmark this question. Show activity on this post. onDestroy() is not always called.

Is onPause always called before onDestroy?

In such a case, though, any cleanup you expected to be done in onPause() and onStop() will not be executed. Although onDestroy() is the last callback in the lifecycle of an activity, it is worth mentioning that this callback may not always be called and should not be relied upon to destroy resources.

What is onDestroy () meant for?

onDestroy: The final call you receive before your 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.


1 Answers

If you want to stop the destroy/create issue that is the default in android because of an orientation change and lock in one orientation then you need to add code and xml

In your activites code (notes about the xml)

    // When an android device changes orientation usually the activity is destroyed and recreated with a new      // orientation layout. This method, along with a setting in the the manifest for this activity     // tells the OS to let us handle it instead.     //     // This increases performance and gives us greater control over activity creation and destruction for simple      // activities.      //      // Must place this into the AndroidManifest.xml file for this activity in order for this to work properly      //   android:configChanges="keyboardHidden|orientation"     //   optionally      //   android:screenOrientation="landscape"     @Override     public void onConfigurationChanged(Configuration newConfig)      {         super.onConfigurationChanged(newConfig);     } 
like image 173
Idistic Avatar answered Nov 09 '22 23:11

Idistic