在这个类中,我覆盖了onSaveInstanceState和onRestoreInstanceState方法,它使我能够在旋转屏幕时保持活动的最后状态,但是,如果我导航到另一个活动,或者我去了我的设备的 Home ,它没有保持最新状态并开始一个新状态。
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable("mylist",mAdapter);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
savedInstanceState.getSerializable("mylist");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
if (menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception ex) {
// Ignore
}
//setHasOptionsMenu(true);
recyclerView = (RecyclerView) findViewById(R.id.card_recycler_view);
if (savedInstanceState != null && savedInstanceState.getSerializable("mylist")!=null) {
savedInstanceState.getSerializable("mylist");
update_START();
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(this, 2);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(mAdapter);
}else {
update_START();
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(this, 2);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(mAdapter);
}
}
任何机构都可以告诉我,即使活动已经被破坏,我应该添加什么来强制我的活动以保持其最新状态,我是否应该向任何其他活动添加任何代码块?
我的onPostExecute方法如下:
@Override
protected void onPostExecute(ArrayList<MovieEntity> result) {
mAdapter=null;
mAdapter = new ImagesAdapter(getBaseContext(), result);
recyclerView.setAdapter(mAdapter);
}
这是我的Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.prof_mohamed.movieapp">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<receiver
android:name=".ConnectivityReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity" android:label="Pop Movies">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".DetailActivity" android:label="MovieDetail"></activity>
</application>
它假设当我在活动之间或甚至在其他应用程序之间导航,然后返回我的第一个正在运行的应用程序时,它应该在开始导航到其他活动或应用程序之前恢复其最新保存的状态。
答案 0 :(得分:0)
首先,您没有正确使用 savedInstanceState.getSerializable(“mylist”),您应该将对象保存并转换为变量并使用它:
列表列表=(列表)savedInstanceState.getSerializable(“mylist”);
此时,仅在onCreate方法中恢复该束(例如,在销毁活动然后重新创建时旋转)
为了支持在活动导航中保存实例状态或按下主页按钮(调用onStop然后onStart),将相同的逻辑放在 onRestoreInstanceState 中的if条件中,该函数在<强>调用onStart 强>
答案 1 :(得分:-1)
在开发者选项中&#34;不要保持活动&#34;选项已启用,您必须禁用它,现在每个东西都在运行,在活动导航,主页按钮导航和屏幕旋转中保存状态除了覆盖这两种方法
@覆盖
protected void onRestoreInstanceState(Bundle savedInstanceState){
super.onRestoreInstanceState(savedInstanceState);
if (savedInstanceState != null && savedInstanceState.getSerializable("mylist")!=null) {
savedInstanceState.getSerializable("mylist");
}
}
@覆盖 protected void onRestoreInstanceState(Bundle savedInstanceState){
super.onRestoreInstanceState(savedInstanceState);
savedInstanceState.getSerializable(&#34; MYLIST&#34);
}
@覆盖 protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
savedInstanceState.getSerializable("mylist");
}
将使您的应用完全保存其状态...