Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
407 views
in Technique[技术] by (71.8m points)

android - onDestroy gets called each time the screen goes on

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...