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


推荐阅读
  • andr ... [详细]
  • 1.如何在运行状态查看源代码?查看函数的源代码,我们通常会使用IDE来完成。比如在PyCharm中,你可以Ctrl+鼠标点击进入函数的源代码。那如果没有IDE呢?当我们想使用一个函 ... [详细]
  • 本文详细介绍了Akka中的BackoffSupervisor机制,探讨其在处理持久化失败和Actor重启时的应用。通过具体示例,展示了如何配置和使用BackoffSupervisor以实现更细粒度的异常处理。 ... [详细]
  • 本文深入探讨了 Java 中的 Serializable 接口,解释了其实现机制、用途及注意事项,帮助开发者更好地理解和使用序列化功能。 ... [详细]
  • Android 渐变圆环加载控件实现
    本文介绍了如何在 Android 中创建一个自定义的渐变圆环加载控件,该控件已在多个知名应用中使用。我们将详细探讨其工作原理和实现方法。 ... [详细]
  • Android LED 数字字体的应用与实现
    本文介绍了一种适用于 Android 应用的 LED 数字字体(digital font),并详细描述了其在 UI 设计中的应用场景及其实现方法。这种字体常用于视频、广告倒计时等场景,能够增强视觉效果。 ... [详细]
  • 本文详细介绍了如何构建一个高效的UI管理系统,集中处理UI页面的打开、关闭、层级管理和页面跳转等问题。通过UIManager统一管理外部切换逻辑,实现功能逻辑分散化和代码复用,支持多人协作开发。 ... [详细]
  • 本章将深入探讨移动 UI 设计的核心原则,帮助开发者构建简洁、高效且用户友好的界面。通过学习设计规则和用户体验优化技巧,您将能够创建出既美观又实用的移动应用。 ... [详细]
  • RecyclerView初步学习(一)
    RecyclerView初步学习(一)ReCyclerView提供了一种插件式的编程模式,除了提供ViewHolder缓存模式,还可以自定义动画,分割符,布局样式,相比于传统的ListVi ... [详细]
  • 本文探讨了如何在给定整数N的情况下,找到两个不同的整数a和b,使得它们的和最大,并且满足特定的数学条件。 ... [详细]
  • 从 .NET 转 Java 的自学之路:IO 流基础篇
    本文详细介绍了 Java 中的 IO 流,包括字节流和字符流的基本概念及其操作方式。探讨了如何处理不同类型的文件数据,并结合编码机制确保字符数据的正确读写。同时,文中还涵盖了装饰设计模式的应用,以及多种常见的 IO 操作实例。 ... [详细]
  • 本文探讨了领域驱动设计(DDD)的核心概念、应用场景及其实现方式,详细介绍了其在企业级软件开发中的优势和挑战。通过对比事务脚本与领域模型,展示了DDD如何提升系统的可维护性和扩展性。 ... [详细]
  • 尽管使用TensorFlow和PyTorch等成熟框架可以显著降低实现递归神经网络(RNN)的门槛,但对于初学者来说,理解其底层原理至关重要。本文将引导您使用NumPy从头构建一个用于自然语言处理(NLP)的RNN模型。 ... [详细]
  • Java编程实践:深入理解方法重载
    本文介绍了Java中方法重载的概念及其应用。通过多个示例,详细讲解了如何在同一类中定义具有相同名称但不同参数列表的方法,以实现更灵活的功能调用。 ... [详细]
  • 本题探讨如何通过最大流算法解决农场排水系统的设计问题。题目要求计算从水源点到汇合点的最大水流速率,使用经典的EK(Edmonds-Karp)和Dinic算法进行求解。 ... [详细]
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社区 版权所有