问题
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?
回答1:
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);
}
回答2:
I had same issue. From ActivityA I called ActivityB. When I closed ActivityB, I expected ActivityA to be shown but it wasn't - it was destroyed. This issues was caused by following attributes of ActivityA in AndroidManifest.xml:
android:excludeFromRecents="true"
android:noHistory="true"
Because of them ActivityA was destroyed after ActivityB was started.
回答3:
mikepenz,in your case if you realy need a android:setorientation = "landscape" means ,you dont need to remove it, just add these set of attribute android:configchanges = "orientation|Screensize" this wont destroy your activity... hope this helps you.
来源:https://stackoverflow.com/questions/6772988/ondestroy-gets-called-each-time-the-screen-goes-on