在Android Essentials的书籍中,他们谈论onSaveInstance和onRestoreInstance,以便在旋转手机或平板电脑后自动恢复放在屏幕上的文本字段中的文本。虽然我们在您旋转屏幕时知道,但应用程序将被销毁并重新创建。
自动恢复不起作用。虽然我在方法onSaveInstance中测试了manualy保存文本,并在onRestoreInstance中设置文本字段中的文本。那也没有用!
我做错了吗?我不能相信它在这些Essentials书中没有用,他们说它确实有效吗?
答案 0 :(得分:0)
这是java代码:
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.util.Log;
import android.widget.EditText;
public class StateChangesActivity extends ActionBarActivity {
private static final String TAG = "wimvangeyt.be.StateChanges";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_state_changes);
Log.i(TAG,"onCreate");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.state_changes, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onStart()
{
super.onStart();
Log.i(TAG,"onStart");
}
@Override
protected void onResume()
{
super.onResume();
Log.i(TAG,"onResume");
}
@Override
protected void onPause()
{
super.onPause();
Log.i(TAG,"onPause");
}
@Override
protected void onStop()
{
super.onStop();
Log.i(TAG,"onStop");
}
@Override
protected void onRestart()
{
super.onRestart();
Log.i(TAG,"onRestart");
}
@Override
protected void onDestroy()
{
super.onDestroy();
Log.i(TAG,"onDestroy");
}
@Override
protected void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
Log.i(TAG,"onSaveInstanceState");
final EditText textBox = (EditText) findViewById(R.id.editText);
CharSequence userText = textBox.getText();
outState.putCharSequence("savedText", userText);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onSaveInstanceState(savedInstanceState);
Log.i(TAG,"onRestoreInstanceState");
final EditText textBox = (EditText) findViewById(R.id.editText);
CharSequence userText = savedInstanceState.getCharSequence("SavedText");
textBox.setText(userText);
}
}
这是xml代码:
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".StateChangesActivity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="142dp"
android:saveEnabled="false"
android:width="200dp"/>
</RelativeLayout>
答案 1 :(得分:0)
U也可以在onCreate()中获取数据。请参阅此内容以获取更多Developer Doc : Recreating an Activity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_state_changes);
Log.i(TAG,"onCreate");
final EditText textBox = (EditText) findViewById(R.id.editText);
if(savedInstanceState != null){
textBox.setText(savedInstanceState.getCharSequence("savedText"););
}
}