好的,我已经覆盖了OnSaveInstanceState和OnRestoreInstanceState,但在OnCreate中,bundle是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>
当我开始活动时,这是我的logcat,然后关闭它,然后再次启动它。
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);
在方法的开头,而不是在结尾,但没有变化。
答案 0 :(得分:1)
此行为看起来是正确的。你说你做的是:
我开始活动,然后关闭它,然后重新开始。
在这种情况下,Android不会恢复您的活动状态。当用户故意关闭活动时,所有状态都被丢弃,当他们稍后启动时,他们会得到一份新的副本。
仅在重新创建活动时恢复状态,因为它已被系统销毁,但用户不希望它被关闭,因此他们希望活动的状态相同。一种这样的情况是屏幕旋转(或通常任何配置改变)。