Issue with use of onSaveInstanceState and onRestor

2019-09-03 04:24发布

问题:

Im using the below code in my shopping cart activity. when i added the items to cart, it shows the items in the cart. but when i open another activity and go back to the cart it shows null. then i found use of onSaveInstanceState and onRestoreInstanceState.. even after i use this code it shows null. can anyone point out what have gone wrong in this code.

what im expecting by use of this code is when i added the items to cart it will save in onSaveInstanceState, and when i open the cart again then it will use onRestoreInstanceState and show the items on the cart.

    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:

I think onRestoreInstanceState is called after onStart and the code for refreshing your UI is in onCreate. So the data is probably there but you don't show it in the UI. Try updating your ListView/Adapter after the line price = savedInstanceState.getString("price");