保存活动状态时方向转变Android [英] Save state of activity when orientation changes android

查看:109
本文介绍了保存活动状态时方向转变Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个aacplayer应用程序,我要救我的活动状态时,方向改变,从纵向到横向,我试图以此来冻结我的TextView的textviews犯规显示为空:

i have an aacplayer app, i want to save the state of my activity when orientation changes from portrait to landscape, the textviews doesnt appears empty i tried to freeze my textview using this:

android:freezesText="true"

我的清单:

android:configChanges="orientation"

我也用这样的:

i used also this:

@Override
    public void onConfigurationChanged(Configuration newConfig){
        super.onConfigurationChanged(newConfig);
        setContentView(R.layout.main2);

所以,当方向将更改为横向,我可以看到我的布局,土地main2.xml,这工作,但我的TextView熄灭,显示为空。流音乐的伟大工程,我可以听它的时候的方向变化,但内部textviews文本每一次我改变我的设备的方向都没有了。

so when orientation changes to landscape i can see my layout-land main2.xml, that works but my textview goes out and appears empty. Stream music works great i can listen to it when orientation changes, but the text inside textviews are gone each time i change the orientation of my device.

我应该怎么做才能解决这个问题?这样我就可以保存所有的状态,我的:

what should i do to fix this?, so i can save the state of all my:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
....
....

非常感谢你。

thank you very much.

推荐答案

当您的方向改变,您不必手动更改的景观布局文件。 Android的做这个自动为您。当方向改变,Android的破坏你现有的活动,并重新创建一个新的活动,这就是为什么你失去的文字。

When your orientation changes, you don't have to manually change to the landscape layout file. Android does this automatically for you. When orientation changes, Android destroys your current activity and creates a new activity again, this is why you are losing the text.

有2个部分,你需要做的,假设你想为纵向和横向单独的布局。

There are 2 parts you need to do, assuming you want a separate layout for portrait and landscape.

  1. 假设你有2个XML布局文件人像和风景,把你的main.xml布局文件在以下文件夹:

  1. Assuming you have 2 XML layout files for portrait and landscape, put your main.xml layout file in the following folders:

RES /布局/ main.xml中< - 这将是你的肖像布局
RES /布局陆/ main.xml中< - 这将是你的景观布置

res/layout/main.xml <-- this will be your portrait layout
res/layout-land/main.xml <-- this will be your landscape layout

这就是所有你需要做的,你没有触摸清单文件修改安卓configChanges =定向或重写 onConfigurationChanged()。其实,这是建议大家不要碰这个你要实现的目标是什么。

That's all you need to do, you don't have to touch the manifest file to modify android:configChanges="orientation" or override the onConfigurationChanged(). Actually, it's recommended you do not touch this for what you are trying to achieve.

现在保存在文本视图文本=)让我们假设你的TextView被命名为MyTextView布局中的XML文件。你的活动需要具备以下条件:

Now to save your text from the text view =) Lets assume your textview is named as MyTextView in your layout xml file. Your activity will need the following:

private TextView mTextView;
private static final String KEY_TEXT_VALUE = "textValue";

@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   mTextView = (TextView) findViewById(R.id.main);
   if (savedInstanceState != null) {
      CharSequence savedText = savedInstanceState.getCharSequence(KEY_TEXT_VALUE);
      mTextView.setText(savedText);
   }
}

@Override
protected void onSaveInstanceState (Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putCharSequence(KEY_TEXT_VALUE, mTextView.getText());
}

基本上,只要Android的破坏并重新创建你的活动为导向的变化,它会调用的onSaveInstanceState()破坏和电话前的onCreate()重建后。不管你保存在包中的onSaveInstanceState中,你可以从的onCreate()参数后面。

Basically, whenever Android destroys and recreates your Activity for orientation change, it calls onSaveInstanceState() before destroying and calls onCreate() after recreating. Whatever you save in the bundle in onSaveInstanceState, you can get back from the onCreate() parameter.

()。如果该项活动的第一次(未由于旋转变化)创建的,所述savedInstanceState将在的onCreate空()。你也可能不需要的安卓freezesText =真正的

So you want to save the value of the text view in the onSaveInstanceState(), and read it and populate your textview in the onCreate(). If the activity is being created for the first time (not due to rotation change), the savedInstanceState will be null in onCreate(). You also probably don't need the android:freezesText="true"

您也可以尝试挽救其他变量,如果你需要,因为你保存你将失去所有的变量时,该活动被破坏并重新创建。

You can also try saving other variables if you need to, since you'll lose all the variables you stored when the activity is destroyed and recreated.

这篇关于保存活动状态时方向转变Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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