热门标签 | 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”这不会破坏你的活动...希望这有助于你。


推荐阅读
  • 1.如何在运行状态查看源代码?查看函数的源代码,我们通常会使用IDE来完成。比如在PyCharm中,你可以Ctrl+鼠标点击进入函数的源代码。那如果没有IDE呢?当我们想使用一个函 ... [详细]
  • 优化ListView性能
    本文深入探讨了如何通过多种技术手段优化ListView的性能,包括视图复用、ViewHolder模式、分批加载数据、图片优化及内存管理等。这些方法能够显著提升应用的响应速度和用户体验。 ... [详细]
  • 本文详细介绍如何使用Python进行配置文件的读写操作,涵盖常见的配置文件格式(如INI、JSON、TOML和YAML),并提供具体的代码示例。 ... [详细]
  • 本文介绍了Java并发库中的阻塞队列(BlockingQueue)及其典型应用场景。通过具体实例,展示了如何利用LinkedBlockingQueue实现线程间高效、安全的数据传递,并结合线程池和原子类优化性能。 ... [详细]
  • 本文详细介绍了Akka中的BackoffSupervisor机制,探讨其在处理持久化失败和Actor重启时的应用。通过具体示例,展示了如何配置和使用BackoffSupervisor以实现更细粒度的异常处理。 ... [详细]
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • Explore how Matterverse is redefining the metaverse experience, creating immersive and meaningful virtual environments that foster genuine connections and economic opportunities. ... [详细]
  • 本文介绍如何使用Objective-C结合dispatch库进行并发编程,以提高素数计数任务的效率。通过对比纯C代码与引入并发机制后的代码,展示dispatch库的强大功能。 ... [详细]
  • 本文将介绍如何使用 Go 语言编写和运行一个简单的“Hello, World!”程序。内容涵盖开发环境配置、代码结构解析及执行步骤。 ... [详细]
  • This guide provides a comprehensive step-by-step approach to successfully installing the MongoDB PHP driver on XAMPP for macOS, ensuring a smooth and efficient setup process. ... [详细]
  • 导航栏样式练习:项目实例解析
    本文详细介绍了如何创建一个具有动态效果的导航栏,包括HTML、CSS和JavaScript代码的实现,并附有详细的说明和效果图。 ... [详细]
  • Java 类成员初始化顺序与数组创建
    本文探讨了Java中类成员的初始化顺序、静态引入、可变参数以及finalize方法的应用。通过具体的代码示例,详细解释了这些概念及其在实际编程中的使用。 ... [详细]
  • 1:有如下一段程序:packagea.b.c;publicclassTest{privatestaticinti0;publicintgetNext(){return ... [详细]
  • 主要用了2个类来实现的,话不多说,直接看运行结果,然后在奉上源代码1.Index.javaimportjava.awt.Color;im ... [详细]
  • 数据库内核开发入门 | 搭建研发环境的初步指南
    本课程将带你从零开始,逐步掌握数据库内核开发的基础知识和实践技能,重点介绍如何搭建OceanBase的开发环境。 ... [详细]
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社区 版权所有