被重新翻上屏幕关闭prevent安卓活动 [英] Prevent Android activity from being recreated on turning screen off

查看:178
本文介绍了被重新翻上屏幕关闭prevent安卓活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何prevent的活动被重新翻上屏幕关闭?

我做什么

  
      
  1. 开始Bejewels并转到珠宝画面。
  2.   
  3. preSS电源按钮不久。屏幕被关闭,但该设备不是
  4.   
  5. preSS电源按钮。
  6.   

我看到

  

在同一屏幕旋转屏幕关闭之前。

在的情况下我的应用程序(小事一桩,只是一个web应用程序有一个单一的web视图)的方案如下:

我做什么

  
      
  1. 在开始我的应用程序。该活动的onCreate()方法加载一个URL到web视图。
  2.   
  3. preSS电源按钮不久。屏幕被关闭,但该设备不是
  4.   
  5. preSS电源按钮。
  6.   

我看到

  

web视图被重新加载页面。

我的期望

  

作为Bejewels情况下,我希望看到同样的画面,这对于我的应用程序是在previous状态的页面:脚本运行,一个表单域   填充等。

调试结果显示,我错了(在previous问题)和的onDestroy()和的onCreate()的画面时,刚满上被称为一个接一个。而且因为我仍然听到音乐,通过一个脚本,当屏幕关闭时,它看起来像的活性和web视图确实存在,直到我再次preSS电源按钮播放。

我试过了。

  1. 机器人:alwaysRetainTaskState =真同样的行为
  2. 在阅读有关意图(没有用,我只是不明白,他们是如何应用到我的情况)。
  3. 使用PhoneGap的。它的表现是不同的:刚刚杀死在pressing电源按钮,整个应用程序。这是更好的,但不是最好的。
解决方案
  1. 创建于Application.onCreate()的观点。
  2. 视图添加到布局中Activity.onCreate()。
  3. 从布局Activity.onDestroy删除视图()。

详情请看这里: <一href="http://stackoverflow.com/questions/10505346/attach-detach-android-view-to-from-layout">Attach/detach Android的视图/从布局

How to prevent an activity from being recreated on turning screen off?

What I do

  1. Start Bejewels and go to the jewelry screen.
  2. Press power button shortly. The screen is turned off, but the device is not.
  3. Press power button again.

What I see

The same screen as before turning screen off.

In case of my application (trivial one, just a web-app with a single WebView) the scenario is the following:

What I do

  1. Start my app. The activity onCreate() method loads an URL into the WebView.
  2. Press power button shortly. The screen is turned off, but the device is not.
  3. Press power button again.

What I see

The WebView is reloading the page.

What I expected

As in Bejewels case I want to see the same screen, that for my app is the page in its previous state: scripts are running, a form fields are filled etc.

Debugging showed, that I was wrong (in a previous question) and onDestroy() and onCreate() are being called one-by-one when the screen is just turned on. And since I still hear a music, played by a script, when the screen is off, it looks like both the activity and the WebView do exist until I press power button again.

What I tried.

  1. android:alwaysRetainTaskState="true" The same behavior.
  2. Reading about intents (to no avail, I just did not understand, how they are applied to my situation).
  3. Using PhoneGap. It behaves differently: just kills the whole application on pressing power button. This IS better, but not the best.

解决方案

  1. Create the view in Application.onCreate().
  2. Add the view to a layout in Activity.onCreate().
  3. Remove the view from the layout in Activity.onDestroy().

Details are here: Attach/detach Android view to/from layout

这篇关于被重新翻上屏幕关闭prevent安卓活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆