每次屏幕亮起时都会调用onDestroy

mik*_*enz 24 android screen onresume ondestroy onpause

每次从屏幕关闭状态返回时,我的应用程序都会被杀死.我获取了我的应用程序所做的所有信息,但我无法找到它调用onDestroy的原因.这是我第一次在我的应用程序中看到这种行为.

我的主要活动扩展了tabActivity,因为它包含tabhost.我已经读过它必须扩展它或它将FC.我不确定我的问题是否与此有关?!哦,它实现了Observer,但这应该没问题.

这是日志:

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
Run Code Online (Sandbox Code Playgroud)

疯狂的是,它在屏幕再次亮起后最多调用onDestroy,有时它有足够的时间在屏幕关闭之前执行此操作.但是在它再次发生之后它再次发生了同样的事情......

我希望有人给我一个提示或有关如何解决此问题的任何信息.

我不确定这是否重要,但我使用android 2.1-update1 sdk作为我的应用程序.


编辑:

该应用程序在真正的Android设备上进行测试.

这是一些基本代码,删除了所有不必要的行和信息:

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);
}
}
Run Code Online (Sandbox Code Playgroud)

EDIT2:

好的,我已经测试了我的应用程序而没有初始化任何东西,然后只有扩展活动,或者没有实现观察者,但我的更改没有任何效果.每次我将手机设置为睡眠状态,然后将其唤醒,接听电话onDestroy()?!


EDIT3:

好的,我发现了一些有趣的东西.

首先是我的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>
Run Code Online (Sandbox Code Playgroud)

一旦我删除了screenOrientation="landscape",每次唤醒我的设备时,应用程序都不会被销毁.我尝试了10次以上但没有更多的电话onDestroy()

所以我认为我必须在代码中设置它?!任何提示或代码片段?

Idi*_*tic 38

如果你想要停止android中的默认的destroy/create问题,因为方向更改并锁定在一个方向,那么你需要添加代码和xml

在你的活动代码中(关于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);
    }
Run Code Online (Sandbox Code Playgroud)

  • 如果您的目标是API版本13或更高版本,则还需要将`| screenSize`添加到清单中的configChanges列表中.http://developer.android.com/guide/topics/resources/runtime-changes.html (22认同)
  • 此外,如果你所做的只是调用无论如何都要调用的`super.onConfigurationChanged()`函数,你真的不需要添加Java代码. (2认同)

Mar*_*lev 8

我有同样的问题.从ActivityA我叫ActivityB.当我关闭ActivityB时,我预计ActivityA会显示但不是 - 它被销毁了.此问题是由AndroidManifest.xml中的ActivityA的以下属性引起的:

android:excludeFromRecents="true"
android:noHistory="true"
Run Code Online (Sandbox Code Playgroud)

因为它们ActivityB在ActivityB启动后被销毁.