在屏幕旋转时保存数据

时间:2016-03-19 08:23:14

标签: java android xml android-layout android-studio

每当屏幕旋转时,我都会尝试保存存储在名为Memo的变量中的数据。我这样做时,我不确定如何保存变量。这是我的Java和我的xml。

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

公共类MainActivity扩展了AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final EditText Input = (EditText) findViewById(R.id.inputLine);
    final TextView Memo = (TextView) findViewById(R.id.memo);
    final Button button = (Button) findViewById(R.id.btnSubmit);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            final String sting = Input.getText().toString();
            Memo.setText(sting);
        }
    });
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    savedInstanceState.putSerializable("string",string);
    // Save UI state changes to the savedInstanceState.
    // This bundle will be passed to onCreate if the process is
    // killed and restarted.
    // etc.
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    // Restore UI state from the savedInstanceState.
    // This bundle has also been passed to onCreate.
    String String = savedInstanceState.getString(string);
}
}

这是我的xml,用于展示我的项目

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="edu.dtcc.bwharto9.rotationslab.MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Please enter your memo below, then press Submit!"
    android:id="@+id/textView"
    android:layout_marginTop="28dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="textMultiLine"
    android:ems="10"
    android:id="@+id/inputLine"
    android:layout_marginTop="40dp"
    android:hint="Enter your memo here"
    android:layout_below="@+id/textView"
    android:layout_centerHorizontal="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Submit"
    android:id="@+id/btnSubmit"
    android:layout_below="@+id/inputLine"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="31dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/memo"
    android:layout_below="@+id/btnSubmit"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="123dp" />
</RelativeLayout>

3 个答案:

答案 0 :(得分:1)

这对我有用,所以我想你可能会试一试:

private int someVarA; //class variable
private String someVarB; //too
private boolean someVarC;//too

@Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
    super.onSaveInstanceState(outState, outPersistentState);
    outState.putInt("someVarA", someVarA);
    outState.putString("someVarB", someVarB);
    outState.putBoolean("someVarC", someVarC);
}
 @Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    someVarA = savedInstanceState.getInt("someVarA");
    someVarB = savedInstanceState.getString("someVarB");
    someVarC = savedInstanceState.getBoolean("someVarc");
}
祝你好运兄弟。

答案 1 :(得分:1)

onClick()方法中,您可以执行以下操作:

final String sting = Input.getText().toString(); 

取而代之的是删除final String以使sting全局化,如下所示:

sting = Input.getText().toString();

然后,确保在您的活动中将字符串声明为全局字符串,不包括以下任何方法:

private String sting = "";

然后,在onRestoreInstanceState(Bundle savedInstanceState)内执行以下操作:

sting = savedInstanceState.getString("string");

同样在onSaveInstanceState(Bundle savedInstanceState)替换

savedInstanceState.putSerializable("string",string);

使用:

savedInstanceState.putString("string",sting);

答案 2 :(得分:0)

/ **将String声明为全局变量* /

 String sting="";  
 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText Input = (EditText) findViewById(R.id.inputLine);
final TextView Memo = (TextView) findViewById(R.id.memo);
final Button button = (Button) findViewById(R.id.btnSubmit);
button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
       sting = Input.getText().toString();
        Memo.setText(sting);
    }
});
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {

    savedInstanceState.putString("string",string);
    // Save UI state changes to the savedInstanceState.
    // This bundle will be passed to onCreate if the process is
    // killed and restarted.
    // etc.
super.onSaveInstanceState(savedInstanceState);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    // Restore UI state from the savedInstanceState.
    // This bundle has also been passed to onCreate.
    String String = savedInstanceState.getString(string);
}
}