变量切换方向损失 [英] Loss of variables switching orientations

查看:215
本文介绍了变量切换方向损失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

林新到Android。我已经写了几个方案,并试图出来我的电话。当我切换方向它像我的手机会重新启动该程序。我所有的变量得到复位。事实上,这并不重置的唯一的事情是,在编辑文本视图的文本。是什么原因造成的?我怎样才能阻止它?我试图寻找它在谷歌和堆栈溢出,但所有我看到的是如何视图方向更改都在一起。我甚至尝试,在一个程序中,与获取文本方法设置我的变量,但不起作用。

解决方案
  

当我切换方向它像我的手机会重新启动该程序。

好是准确的,当前可见的活动完全被破坏并重新创建。你的'应用'的其他部件可以或可以不受到影响。

  

是什么原因造成的?我怎样才能阻止它?

这是由设计,为阻止它,你可能会或可能不会想这样做。

正如已经提到的,可以指定要处理'配置'的变化(如方向)自己,甚至强行只有一个(例如,横向或纵向)。

在许多情况下,但是,一个应用程序开发者可以选择改变基于设备是否是在一个方向或另一个的布局。一些布局可能会很好地工作在画像,但不能在横向(或反之亦然)和设计方法的目的(消灭/重新创建当前可见活动)是指,以容纳这一点。

如果一个开发选择不处理配置更改自理或强制特定的方向,正确的方式来处理事情是确保在动荡UI元素(如EditTexts)输入的所有数据都正确保存后重新创建方向的改变。

要做到这一点,活动的理解生命周期是必要的,因为正在使用的各种活动的被称为整个生命周期的方法来保存/恢复数据。

基本阅读...

运行时更改

活动生命周期

应用程序基础

Im new to android. I have written a couple of programs and tried them out on my phone. When I switch orientations its like my phone restarts the program. All my variables get reset. In fact the only thing that does not reset is the text that is in the edit text views. What causes this? How can I stop it? I have tried looking it on google and on stack overflow but all I am seeing is how to the view orientation from changing all together. I even tried, in one program, to set my variables with the get text method but this does not work.

解决方案

When I switch orientations its like my phone restarts the program.

Well to be accurate, the currently visible Activity is completely destroyed and recreated. Other components of your 'application' may or may not be impacted.

What causes this? How can I stop it?

It's by design and as for stopping it, you may or may not want to do that.

As has been mentioned, it is possible to specify that you want to handle 'configuration' changes (such as orientation) yourself or even force only one (e.g., landscape or portrait).

In many cases, however, an app developer may choose to change a layout based on whether a device is in one orientation or another. Some layouts may work fine in portrait but not in landscape (or vice versa) and the aim of the designed approach (to destroy / recreate the current visible Activity) is meant to accommodate that.

If a dev chooses not to handle the configuration changes themself or force a specific orientation, the correct way to handle things is to make sure all data entered in 'volatile' UI elements (such as EditTexts) are correctly saved and recreated after orientation change.

To do this, understanding of the Activity life-cycle is essential as is making use of the various Activity methods that are called throughout the life-cycle to save / restore data.

Essential reading...

Runtime Changes

Activity Lifecycle

Application Fundamentals

这篇关于变量切换方向损失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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