更改屏幕方向时,为什么我的数据不能正确显示?

时间:2018-07-29 16:24:51

标签: android

我是Android的新手,目前正在尝试复习不同的任务,同时正在关注有关Pluralsight的教程。

我的问题是关于屏幕方向的,当发生屏幕方向事件但我的Button和TextView的内容未显示时,我已经正确使用了onRestoreInstanceState()onSaveInstanceState()函数来存储/获取数据正确地。 (请参见下图)

旋转屏幕之前:

Before entering Data

After entering Data

屏幕旋转后:

This is when my problem occurs

Then If I touch the button again

在我关注的教程/视频中没有发生这种情况。我已经彻底比较了我们的两个代码,我无法弄清楚我的出了什么问题。

这是我的MainActivity.java:

    package com.example.ralberti.method1;

import android.os.PersistableBundle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private final String TAG = MainActivity.class.getSimpleName();

    private EditText etName;
    private TextView txtMessage;
    private Button btnSubmit;

    // Key values
    private final String KEY_MESSAGE = "message";
    private final String KEY_BTN_TEXT = "button_text";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i(TAG,"onCreate()");

        etName = findViewById(R.id.etName);
        txtMessage = findViewById(R.id.txtView);
        btnSubmit = findViewById(R.id.btnSubmit);

        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                txtMessage.setText("Welcome "+ etName.getText().toString());
                btnSubmit.setText("LOGOUT");
            }
        });

        if (savedInstanceState != null){
            btnSubmit.setText(savedInstanceState.getString(KEY_BTN_TEXT));
            txtMessage.setText(savedInstanceState.getString(KEY_MESSAGE));
        }
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        Log.i(TAG,"onRestoreInstanceState()");


    }

    @Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
        super.onSaveInstanceState(outState, outPersistentState);
        Log.i(TAG,"onSaveInstanceState()");

        outState.putString(KEY_MESSAGE, txtMessage.getText().toString());
        outState.putString(KEY_BTN_TEXT, btnSubmit.getText().toString());
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.i(TAG,"onRestart()");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.i(TAG,"onStart()");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.i(TAG,"onPause()");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.i(TAG,"onResume()");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.i(TAG,"onStop()");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i(TAG,"onDestroy()");
    }

}

这是activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/etName"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="59dp"
        android:gravity="center"
        android:hint="@string/enter_your_name"
        android:inputType="textPersonName"/>

    <Button
        android:id="@+id/btnSubmit"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:text="@string/login"
        android:layout_below="@+id/etName"
        android:layout_marginTop="10dp"
        android:layout_centerHorizontal="true"/>

    <TextView
        android:id="@+id/txtView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/login_message"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
        android:textColor="#bab8b8"
        android:textSize="30sp"
        android:textStyle="bold"
        android:layout_below="@+id/btnSubmit"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"/>

</RelativeLayout>

我已经使用Nexus S API 28虚拟设备和我自己的设备HTC Desire 650 API 23进行了这些测试,结果是相同的。

如果有人可以告诉我这个问题在哪里发生,并向我解释原因,那就太好了! 我不仅要解决此问题,还要了解正在发生的事情。 :)

2 个答案:

答案 0 :(得分:0)

({onSaveInstanceState()onRestoreInstanceState)有2种实现方式
第一种实施方式:

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
}

@Override
public void onSaveInstanceState(Bundle outState) {
}

第二种实现方式:

@Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {    
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState, PersistableBundle persistentState) {
}

您的代码包括第二个实现中的onSaveInstanceState和第一个中的onRestoreInstanceState。这是行不通的。 在onSaveInstanceState()中,将getString替换为putString

@Override
public void onSaveInstanceState(Bundle outState) {
    Log.i("TAG","onSaveInstanceState()");

    outState.putString(KEY_MESSAGE, txtMessage.getText().toString());
    outState.putString(KEY_BTN_TEXT, btnSubmit.getText().toString());

    super.onSaveInstanceState(outState);
}

答案 1 :(得分:0)

通常,基本的Activity代码会在屏幕旋转时自动恢复正常视图的状态(如EditText)。您可以尝试完全删除onSaveInstanceState中的自定义代码以及在onCreate中应用状态的位置,以查看会发生什么。

如果您希望保持原样,请在捆绑参数后将调用移至super.onSaveInstanceState,例如this example

@Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
    Log.i(TAG,"onSaveInstanceState()");

    outState.putString(KEY_MESSAGE, txtMessage.getText().toString());
    outState.putString(KEY_BTN_TEXT, btnSubmit.getText().toString());

    // call this after you've added your custom data
    super.onSaveInstanceState(outState, outPersistentState);
}

您还可以尝试覆盖public void onSaveInstanceState(Bundle outState)而不是public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState),因为无论如何您都不使用第二个参数。