使用onSaveInstanceState和onRestoreInstanceState时出现问题

时间:2015-02-20 09:12:10

标签: android onrestoreinstancestate

我在购物车活动中使用以下代码。当我将商品添加到购物车时,它会显示购物车中的商品。但是,当我打开另一个活动并返回购物车时,它显示为null。然后我发现使用onSaveInstanceStateonRestoreInstanceState ..即使在我使用此代码后它显示为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;
                }
            });

1 个答案:

答案 0 :(得分:0)

我认为在onStart之后调用onRestoreInstanceState,刷新UI的代码在onCreate中。所以数据可能存在,但是你没有在UI中显示它。尝试在行price = savedInstanceState.getString("price");

之后更新ListView / Adapter