Android 使用onSaveInstanceState和onRestoreInstanceState时出现问题

Android 使用onSaveInstanceState和onRestoreInstanceState时出现问题,android,onrestoreinstancestate,Android,Onrestoreinstancestate,我在我的购物车活动中使用以下代码。当我将项目添加到购物车时,它会显示购物车中的项目。但当我打开另一个活动并返回购物车时,它显示为空。然后我找到了onSaveInstanceState和onRestoreInstanceState的用法。。即使在我使用这段代码之后,它也显示为null。谁能指出这段代码出了什么问题 使用此代码时,我所期望的是,当我将项目添加到购物车时,它将保存在onSaveInstanceState中,当我再次打开购物车时,它将使用onRestoreInstanceState并在购

我在我的购物车活动中使用以下代码。当我将项目添加到购物车时,它会显示购物车中的项目。但当我打开另一个活动并返回购物车时,它显示为空。然后我找到了onSaveInstanceState和onRestoreInstanceState的用法。。即使在我使用这段代码之后,它也显示为null。谁能指出这段代码出了什么问题

使用此代码时,我所期望的是,当我将项目添加到购物车时,它将保存在onSaveInstanceState中,当我再次打开购物车时,它将使用onRestoreInstanceState并在购物车上显示项目

    public class ShoppingCartActivity extends Activity {

        ProductAdapter mCartList;
        ExpandableListView expListView;
        List<String> listDataHeader;
        List<String> listDataHeaderPrice;
        List<String> listDataHeaderQty;
        HashMap<String, List<String>> listDataChild;
        private ExpandableListView mExpandableList;
        String description;
        String price;
        String quantity;
        ArrayList<String> myList = new ArrayList<String>();

        @Override
        public void onSaveInstanceState(Bundle savedInstanceState) {
            savedInstanceState.putString("description", description);
            savedInstanceState.putString("price", price);
            savedInstanceState.putString("quantity", quantity);
            super.onSaveInstanceState(savedInstanceState);
        }

        @Override
        public void onRestoreInstanceState(Bundle savedInstanceState) {
          super.onRestoreInstanceState(savedInstanceState);
          // Restore UI state from the savedInstanceState.
          // This bundle has also been passed to onCreate.

          description = savedInstanceState.getString("description");
          quantity = savedInstanceState.getString("quantity");
          price = savedInstanceState.getString("price");
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.cart_activity);

if (savedInstanceState != null) {
            Toast.makeText(getApplicationContext(), "1", Toast.LENGTH_SHORT)
                    .show();
        } else {
            Toast.makeText(getApplicationContext(), "2", Toast.LENGTH_SHORT)
                    .show();
        }

            quantity = getIntent().getStringExtra("quantity");
            price = getIntent().getStringExtra("price");
            description = getIntent().getStringExtra("description");

            myList.add(description);
            myList.add(price);
            myList.add(quantity);


            ActionBar actionBar = getActionBar();
            getActionBar().setIcon(
                    new ColorDrawable(getResources().getColor(
                            android.R.color.transparent)));
            getWindow().setSoftInputMode(
                    WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
            // Enabling Back navigation on Action Bar icon
            actionBar.setDisplayHomeAsUpEnabled(true);

            // get the listview
            expListView = (ExpandableListView) findViewById(R.id.lvExp);

            // preparing list data
            prepareListData();

            mCartList = new ProductAdapter(this, listDataHeader,
                    listDataHeaderPrice, listDataHeaderQty, listDataChild);

            // setting list adapter
            expListView.setAdapter(mCartList);

            // Listview Group click listener
            expListView.setOnGroupClickListener(new OnGroupClickListener() {

                @Override
                public boolean onGroupClick(ExpandableListView parent, View v,
                        int groupPosition, long id) {
                    Toast.makeText(getApplicationContext(),
                            "Group Clicked " + listDataHeader.get(groupPosition),
                            Toast.LENGTH_SHORT).show();
                    return false;
                }
            });

我认为onRestoreInstanceState是在onStart之后调用的,用于刷新UI的代码在onCreate中。因此,数据可能在那里,但您不会在UI中显示它。尝试在price=savedInstanceState.getStringprice行之后更新ListView/适配器

如果savedInstanceState!=无效的{..当我重新打开该活动时,它将转到并打印2。是否有方法将savedInstanceState传递给onCreate?onSaveInstanceState将在活动被终止时调用,以回收资源。因此不需要调用它。我想调用它,因此如何终止它?您不需要终止活动。如果android need资源它将终止您的活动并调用onSaveInstanceState方法保存您想要的内容。但是,如果您返回到活动,它将维护所有UI字段和变量。如果您在移动到另一个活动之前对活动调用finish,它将销毁您当前的活动。onSaveInstanceState保存所有数据,但当我下次来到活动时,它会变为null,restore或oncreate方法不会识别保存在onSaveInstanceState中的值,所以最后它显示每次离开当前活动时都会调用nullonSaveInstanceState?