我知道有类似的问题已被提出,但这些问题的解决方案都没有对我有用。
当我继续尝试保存应用程序的状态时,EditText视图的状态不会被保存和恢复。我继续并对所有内容进行了评论,只是放入一个临时字符串进行保存,但当应用程序再次加载时,onCreate()方法不会打印'恢复实例状态'
package com.fwumdesoft.udppacketsender;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
/**
* Posts a UDP message with the given data to the
* target address on the target port.
*/
class UdpPostActivity extends AppCompatActivity {
private static final String TAG = "UdpPostActivity";
private static final String stateTextTargetHost = "com.fwumdesoft#TargetHost";
private static final String stateTextTargetPort = "com.fwumdesoft#TargetPort";
private static final String stateTextHexData = "com.fwumdesoft#HexData";
private static final String stateTextStringData = "com.fwumdesoft#StringData";
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.v(TAG, "onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_udp_post);
if (savedInstanceState != null) {
// ((EditText) findViewById(R.id.txtAddress)).setText(savedInstanceState.getString(stateTextTargetHost));
// ((EditText) findViewById(R.id.txtPort)).setText(savedInstanceState.getString(stateTextTargetPort));
// ((EditText) findViewById(R.id.txtData)).setText(savedInstanceState.getString(stateTextHexData));
// ((EditText) findViewById(R.id.txtStringData)).setText(savedInstanceState.getString(stateTextStringData));
String text = savedInstanceState.getString(stateTextStringData);
Log.v(TAG, "Restoring instance state");
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
// outState.putString(stateTextTargetHost, ((EditText)findViewById(R.id.txtAddress)).getText().toString());
// outState.putString(stateTextTargetPort, ((EditText)findViewById(R.id.txtPort)).getText().toString());
// outState.putString(stateTextHexData, ((EditText)findViewById(R.id.txtData)).getText().toString());
// outState.putString(stateTextStringData, ((EditText)findViewById(R.id.txtStringData)).getText().toString());
super.onSaveInstanceState(outState);
outState.putString(stateTextStringData, "test");
Log.v(TAG, "Saved instance state");
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
Log.v(TAG, "onRestore");
// ((EditText)findViewById(R.id.txtAddress)).setText(savedInstanceState.getString(stateTextTargetHost));
// ((EditText)findViewById(R.id.txtPort)).setText(savedInstanceState.getString(stateTextTargetPort));
// ((EditText)findViewById(R.id.txtData)).setText(savedInstanceState.getString(stateTextHexData));
// ((EditText)findViewById(R.id.txtStringData)).setText(savedInstanceState.getString(stateTextStringData));
}
在logcat输出中,我得到了"保存的实例状态"然后" onCreate"但我没有得到"恢复实例状态"或" onRestore"重新启动应用时。
答案 0 :(得分:0)
在检查savedInstanceState是否为空时,你应该参考你在outState中给出的密钥,就像这样。
if (savedInstanceState != null) {
String text = savedInstanceState.getString("test");
Log.v(TAG, "Restoring instance state");
}
本文档可能会有所启发
https://developer.android.com/guide/components/activities/activity-lifecycle.html
答案 1 :(得分:0)
进入调试模式,查看是否正在调用onCreate。你的清单中是否设置了android:configChanges?