我在Android Studio中创建了一个示例应用程序,以了解Android应用程序的生命周期。我知道方向改变完全重新开始活动(即再次调用OnCreate方法)。 据我所知,方向改变应该已经破坏了上下文,并在设备旋转后显示一个空白文本。但不知怎的,没有重写onSaveInstanceState和onRestoreInstanceState方法,它保存了上下文。EditText自动保存设备旋转后的值
我没有任何碎片。它只是Android Studio提供的基本模板,几乎没有重载的生命周期方法。 这里是我的MainActivity类别:
package com.example.android.a2_screen_orientation_change;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onStart() {
super.onStart();
Log.i(TAG, "in method onStart");
}
@Override
protected void onResume() {
super.onResume();
Log.i(TAG, "in method onResume");
}
@Override
protected void onRestart() {
super.onRestart();
Log.i(TAG, "in method onRestart");
}
@Override
protected void onPause() {
super.onPause();
Log.i(TAG, "in method onPause");
}
@Override
protected void onStop() {
super.onStop();
Log.i(TAG, "in method onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.i(TAG, "in method onDestroy");
}
}
布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
tools:context="com.example.android.a2_screen_orientation_change.MainActivity">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
AbdroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.a2_screen_orientation_change">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这个活动怎么样是导致您认为国家是不会回到原点?请在你的问题中列出这样的状态。 –
等待,我将添加截图 –
'EditText's自动保存/恢复保存状态包中的文本。你不需要做任何事情就可以发生。 https://developer.android.com/reference/android/widget/TextView.html#setFreezesText(boolean)(注意“冰柱”是保存状态包的旧术语。) –