我正在开发一个有四个活动的基本应用程序。前两个活动具有文本视图,在按下后退按钮时将重置这些文本视图。这是清单
<activity android:name=".MainActivity"
android:alwaysRetainTaskState="true"
android:launchMode="singleInstance">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ContactActivity" />
<activity android:name=".Page2Activity"
android:alwaysRetainTaskState="true"
android:launchMode="singleInstance"/>
<activity android:name=".FinalActivity"></activity>
我也用过android:freezesText="true",但它没有帮助
我没有写任何安卓程序。但是你应该试试android:alwaysRetainTaskState:"false"活动所处的任务状态是否始终由系统维护 — 如果为"true",则为"true",如果允许系统在某些情况下将任务重置为其初始状态,则为"false"。
您的解决方案可能是以字符串形式保存数据,并在恢复时重新填充对象。您可以将数据保存在 onPause(( 中,然后在 onResume(( 中重新填充。我将向您展示如何使用共享首选项来做到这一点。共享首选项是一种保存字符串、整数、列表和其他对象的简单方法,无需在 android 中建立数据库。
//This code will save a string. The first parameter in putString() is the key. The second is the value
SharedPreferences.Editor editor = getSharedPreferences("myPrefs", MODE_PRIVATE).edit();
editor.putString("myTextviewText", "Hello World");
//Save the data
editor.apply();
//This code will retrieve the String. You can run this code and retrieve the value even if the app was killed
SharedPreferences prefs = getSharedPreferences("myPrefs", MODE_PRIVATE);
String restoredText = prefs.getString("myTextViewText", "default");
您可以覆盖 onPause,并在其中使用代码的第一部分将您想要的内容保存在文本视图中。我在简历使用第二部分检索字符串,然后使用恢复的文本编辑文本视图文本。要保存服务器值,只需使用不同的键来设置和获取数据。
共享首选项将在应用程序停止时保存。它们的作用类似于数据库,但维护较少。