旋转时保存和恢复文本

Saving and restoring text when rotating

我已经看到了一些关于如何实现这一点的例子,但我仍然无法让它工作。

我遵循了 this 示例,但是 TextViewOnCreate 和旋转屏幕时是空的。以下是我的实现方式:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_image_viewer);

    displayFileName = (TextView) findViewById(R.id.displayFileName);

    //Getting the fileName
    Bundle extras = getIntent().getExtras();
    uri = extras.getString("uriAsString");
    Uri myUri = Uri.parse(uri);
    filename = uri.substring(uri.lastIndexOf("/") + 1);
    nameWitoutExtention = filename.substring(0, filename.lastIndexOf('.'));

    //Checking if Text was stored
    if (savedInstanceState != null) {
        CharSequence savedText = savedInstanceState.getCharSequence(KEY_TEXT_VALUE);
        displayFileName.setText(savedText);
    }

}

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

我想要实现的是将 nameWitoutExtention 设置为 displayFileName 并且当设备旋转时让文本保持不变。

我在文本上得到了一个空指针,这就是我尝试这个的原因,对我做错了什么有帮助吗?


编辑:

目前我的实现是这样的:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_image_viewer);

    displayFileName = (TextView) findViewById(R.id.displayFileName);

    if(savedInstanceState != null){
        CharSequence savedText = savedInstanceState.getCharSequence(KEY_TEXT_VALUE);

        //When displaying this toast the correct filename gets displayed
        //Toast.makeText(getApplicationContext(), savedText, Toast.LENGTH_LONG).show();

        displayFileName.setText(savedText+"");
    }
    else{
        //Getting the fileName
        Bundle extrsas = getIntent().getExtras();
        uri = extrsas.getString("selectedStudentVid");
        Uri mUri = Uri.parse(uri);
        filename = uri.substring(uri.lastIndexOf("/") + 1);
        nameWitoutExtention = filename.substring(0, filename.lastIndexOf('.'));
        displayFileName.setText(nameWitoutExtention);
    }

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

我也按照@PorasBhardwaj

的建议尝试了onRestoreInstanceState()

我遇到的异常是:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

尝试这样做:

//Checking if Text was stored
 if(savedInstanceState != null){
       CharSequence savedText = 
      savedInstanceState.getCharSequence(KEY_TEXT_VALUE);
      displayFileName.setText(savedText + "");
 }
else{
 //Getting the fileName
Bundle extras = getIntent().getExtras();
uri = extras.getString("uriAsString");
Uri myUri = Uri.parse(uri);
filename = uri.substring(uri.lastIndexOf("/") + 1);
nameWitoutExtention = filename.substring(0, filename.lastIndexOf('.'));
}

你做错的是。每当方向改变时,它会再次从 extras 获取值,该值在旋转改变后为空,并将相同的值传递给它。这就是你得到例外的原因。 试试这个希望它会帮助你。

尝试使用onRestoreInstanceState()回调方法获取保存的数据。

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
 // TODO Auto-generated method stub
 super.onRestoreInstanceState(savedInstanceState);
 CharSequence savedText = savedInstanceState.getCharSequence(KEY_TEXT_VALUE);
 displayFileName.setText(savedText);
}

根据官方文档。

Most implementations will simply use onCreate(Bundle) to restore their state, but it is sometimes convenient to do it here after all of the initialization has been done or to allow subclasses to decide whether to use your default implementation.

因此,为了获得最佳实践,请在 onCreate 中保留视图层次结构,并在 onRestoreInstanceState 中恢复之前的状态。