好的,我同时覆盖了OnSaveInstanceState和OnRestoreInstanceState,但是在OnCreate中,捆绑包为null。
我已经解决了所有其他相关问题,但是它们对我没有帮助。
这是我的主要活动。
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
List<Bundle> bundles;
Bundle bundle;
CustomAdapter customAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(savedInstanceState==null) {
Log.i("abc3","savedInstanceState is null, called in OnCreate");
bundle = new Bundle();
bundle.putInt("productImage", R.drawable.happy);
bundle.putString("nameOfProduct", "iPhone0");
bundle.putString("productAmount", "5000");
bundle.putString("nameOfDestination", "DGBDGHIDAFGK");
bundle.putString("addressOfDestination", "Velachery Main Road, Near IIT Madras, Chennai,Tamil Nadu,India 600042");
bundle.putString("contactno", "0123456789");
bundle.putBoolean("isSeller", true);
bundles = new ArrayList<>();
bundles.add(0, bundle);
/*Log.i("abc1", bundle.getString("nameOfProduct"));
bundle.remove("nameOfProduct");
bundle.putString("nameOfProduct", "iPhone1");*/
Bundle bundle1 = new Bundle();
bundle1.putInt("productImage", R.drawable.happy);
bundle1.putString("nameOfProduct", "iPhone1");
bundle1.putString("productAmount", "5000");
bundle1.putString("nameOfDestination", "DGBDGHIDAFGK");
bundle1.putString("addressOfDestination", "Velachery Main Road, Near IIT Madras, Chennai,Tamil Nadu,India 600042");
bundle1.putString("contactno", "0123456789");
bundle1.putBoolean("isSeller", true);
bundles.add(1, bundle1);
/*Log.i("abc1", bundle.getString("nameOfProduct"));
bundle.remove("nameOfProduct");
bundle.putString("nameOfProduct", "iPhone2");*/
Bundle bundle2 = new Bundle();
bundle2.putInt("productImage", R.drawable.happy);
bundle2.putString("nameOfProduct", "iPhone2");
bundle2.putString("productAmount", "5000");
bundle2.putString("nameOfDestination", "DGBDGHIDAFGK");
bundle2.putString("addressOfDestination", "Velachery Main Road, Near IIT Madras, Chennai,Tamil Nadu,India 600042");
bundle2.putString("contactno", "0123456789");
bundle2.putBoolean("isSeller", true);
bundles.add(2, bundle2);
}
else
{
Log.i("abc3","savedInstanceState is not null,called in OnCreate");
for(int i=0;savedInstanceState.containsKey(String.valueOf(i));++i)
bundles.add(savedInstanceState.getBundle(String.valueOf(i)));
}
/*Log.i("abc1", bundle.getString("nameOfProduct"));*/
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
customAdapter= new CustomAdapter(this,bundles,recyclerView);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(customAdapter);
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
bundles = customAdapter.getBundleList();
// Log.i("abc3", String.valueOf(bundles.size()));
for(int i=0;i<bundles.size();++i)
{savedInstanceState.putBundle(String.valueOf(i),bundles.get(i));}
super.onSaveInstanceState(savedInstanceState);
if(savedInstanceState==null)Log.i("abc3","savedInstanceState is null, called in onSaveInstanceState");
else Log.i("abc3","savedInstanceState is not null, called in onSaveInstanceState");
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState)
{
if(savedInstanceState!=null)
for(int i=0;savedInstanceState.containsKey(String.valueOf(i));++i)
bundles.add(savedInstanceState.getBundle(String.valueOf(i)));
super.onRestoreInstanceState(savedInstanceState);
}
}
和我的清单
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.Light.DarkActionBar">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
这是我开始活动时的日志猫,然后将其关闭,然后再次启动。
06-05 12:56:46.777 27184-27184/com.example.sharath.delivery_app I/abc3: savedInstanceState is null, called in OnCreate
06-05 12:56:53.384 27184-27184/com.example.sharath.delivery_app I/abc3: savedInstanceState is not null, called in onSaveInstanceState
06-05 12:57:00.613 27443-27443/com.example.sharath.delivery_app I/abc3: savedInstanceState is null, called in OnCreate
我试过打电话
super.onSaveInstanceState(savedInstanceState);
在方法的开头,而不是在方法的结尾,也没有变化。
最佳答案
此行为看起来是正确的。您说您要做的是:
我开始活动,然后关闭它,然后再次开始。
在这种情况下,Android不会恢复您的活动状态。当用户有意关闭活动时,所有状态都将被丢弃,而当他们以后开始时,它们将获得新的副本。
仅在重新创建活动时才恢复该状态,因为该活动已被系统破坏,但用户不希望其关闭,因此他们希望该活动的状态相同。屏幕旋转(或通常是任何配置更改)就是这种情况之一。