热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

Myapplicationgetskilledeachtimethatitcomesbackfromthescreen-off-state.Ifetchallthei

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.

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

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.

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

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...

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

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.

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


EDIT:

The application gets tested on a real Android Device.

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

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?!

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


EDIT3:

Ok, I found out something interesting.

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

First here's my AndroidManifest.xml

首先是我的AndroidManifest.xml



    

    
    
    

    
        
            
                
                
            
        
    

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()

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

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

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

3 个解决方案

#1


38  

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

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

In your activites code (notes about the 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);
    }

#2


8  

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:

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

android:excludeFromRecents="true"
android:noHistory="true"

Because of them ActivityA was destroyed after ActivityB was started.

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

#3


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.

mikepenz,在你的情况下,如果你真的需要一个android:setorientation =“landscape”意味着,你不需要删除它,只需添加这些属性android:cOnfigchanges=“orientation | Screensize”这不会破坏你的活动...希望这有助于你。


推荐阅读
author-avatar
sdfasdfqg
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有