title: Updating cart count on pressing of back button after deletion
body: <p>I have two activities, one is <code>UserActivity</code> and other one is <code>CartActivity</code> I am showing a list of products in <code>UserActivity</code>. On click of a button <code>AddtoCart</code> I am adding the products to the cart. I am facing this problem:</p>
  <p>When I add click the button <code>AddtoCart</code> there is a cart icon in the action bar and I have a custom layout of a <code>textview</code> showing cart counter on that cart icon. That counter updates whenever I click the button <code>AddtoCart</code>. Now I move on to the <code>CartActivity</code> and delete some of the products from the cart. When I press back button now to return to <code>UserActivity</code>, the counter text view doesn't update.</p>
  <p>I have read about few ways of doing the updation on pressing of back as given in the question here <a href="https://stackoverflow.com/questions/5545217/back-button-and-refreshing-previous-activity">Back button and refreshing previous activity</a>. Two methods given in the answer are by overriding the <code>OnResume()</code> method of <code>UserActivity</code> or by starting activity for result.</p>
  <p>I think I need to pass a variable called <code>DeleteCounter</code> from <code>CartActivity</code> to the <code>UserActivity</code> when I press the back button and subtract it from the original number of products in the Counter <code>TextView</code> and update the text view.</p>
  <p>Here is the partial code of <code>UserActivity</code> and I have the function to update cart counter in this code only which is called when I click a button. Also the code of <code>onActivityResult()</code> in commented in this which I tried from the answer of above given SO question link. It's not working out:</p>
  <pre><code>    public class UserActivity extends AppCompatActivity{
              private int cartindex = 0;
              private TextView counterTV = null;
              
              @Override
              protected void onCreate(Bundle savedInstanceState) {
                  super.onCreate(savedInstanceState);
                  setContentView(R.layout.activity_user);
                
                  Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
                  setSupportActionBar(toolbar);
              }
  //    @Override
  //    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  //        if (requestCode == 1) {
  //
  //            if(resultCode == RESULT_OK){
  //                  Intent intent = getIntent();
  //                  Integer deletecounter = intent.getIntExtra(&quot;DeleteCounter&quot;,0);
  //                  if(deletecounter&gt;0){
  //                      UpdateCartCount(Integer.parseInt(counterTV.getText().toString())-deletecounter);
  //                  }
  //            }
  //        }
  //    }
              
          @Override
              public boolean onCreateOptionsMenu(Menu menu) {
                  // Inflate the menu; this adds items to the action bar if it is present.
                  getMenuInflater().inflate(R.menu.user, menu);
                  final View menu_list = menu.findItem(R.id.action_hawk).getActionView();
                  counterTV = (TextView) menu_list.findViewById(R.id.cartcounter);
                  UpdateCartCount(cartindex);
                  new MyMenuItemStuffListener(menu_hotlist, &quot;Show message&quot;) {
                      @Override
                      public void onClick(View v) {
                          Intent intent= new  Intent(UserActivity.this,CartActivity.class);
                          intent.putExtra(&quot;ProductTitle&quot;,pname);
                          intent.putExtra(&quot;ProductUrl&quot;,purl);
                          intent.putExtra(&quot;ProductPrice&quot;,pprice);
                          intent.putExtra(&quot;BargainPrice&quot;,bargainprice);
                          UserActivity.this.startActivity(intent);
                      }
                  };
                  return true;
      }
      //Function to update cart count
              public void UpdateCartCount(final int new_number) {
                      cartindex = new_number;
                      if (counterTV == null) return;
                      runOnUiThread(new Runnable() {
                          @Override
                          public void run() {
                              if (new_number == 0)
                                  counterTV.setVisibility(View.INVISIBLE);
                              else {
                                  counterTV.setVisibility(View.VISIBLE);
                                  counterTV.setText(Integer.toString(new_number));
                              }
                          }
                      });
                  }
  </code></pre>
  <p>Here is the code of <code>CartActivity</code>:</p>
  <pre><code>    public class CartActivity extends AppCompatActivity {
      
          private List&lt;Product&gt; mCartList;
          private ProductAdapter mProductAdapter;
          private static List&lt;Product&gt; cart;
          private static Integer deletecounter= 0;
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_cart);
              Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
              setSupportActionBar(toolbar);
              mCartList = getCart();
      
      
              Intent intent = getIntent();
              String ProductTitle = intent.getStringExtra(&quot;ProductTitle&quot;);
              String ProductUrl = intent.getStringExtra(&quot;ProductUrl&quot;);
              String ProductPrice = intent.getStringExtra(&quot;ProductPrice&quot;);
              String BargainPrice = intent.getStringExtra(&quot;BargainPrice&quot;);
              Product product = new Product(ProductTitle, ProductUrl, ProductPrice, BargainPrice);
              mCartList.add(product);
              // Make sure to clear the selections
              for (int i = 0; i &lt; mCartList.size(); i++) {
                  mCartList.get(i).selected = false;
              }
      
              // Create the list
              final ListView listViewCatalog = (ListView) findViewById(R.id.cart_list_view);
              mProductAdapter = new ProductAdapter(mCartList, getLayoutInflater(), true, CartActivity.this);
              listViewCatalog.setAdapter(mProductAdapter);
      
              listViewCatalog.setOnItemClickListener(new AdapterView.OnItemClickListener() {
      
                  @Override
                  public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position,
                                          long id) {
      
                      Product selectedProduct = mCartList.get(position);
                      if (selectedProduct.selected)
                          selectedProduct.selected = false;
                      else
                          selectedProduct.selected = true;
      
                      mProductAdapter.notifyDataSetInvalidated();
      
                  }
              });
              FloatingActionButton Delete = (FloatingActionButton) findViewById(R.id.fab);
              delete.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View view) {
                      // Loop through and remove all the products that are selected
                      // Loop backwards so that the remove works correctly
      
                      for (int i = mCartList.size() - 1; i &gt;= 0; i--) {
      
                          if (mCartList.get(i).selected) {
                              mCartList.remove(i);
                              deletecounter++;
                          }
                      }
  // THIS IS THE CODE I USED TO RETURN DATA TO PREVIOUS ACTIVITY BUT UserActivity STARTS AUTOMATICALLY AFTER DELETION OF SELECTED PRODUCTS AS SOON AS I CLICK THE DELETE BUTTON EVEN WHEN THERE ARE PRODUCTS IN THE CART.
   //                  if(deletecounter!=0) {
  //                    Intent i = new Intent(HawkActivity.this, UserActivity.class);
  //                    startActivityForResult(i, 1);
  //                    Intent returnIntent = new Intent();
  //                    returnIntent.putExtra(&quot;DeleteCounter&quot;, deletecounter);
  //                    setResult(RESULT_OK, returnIntent);
  //                }
                      mProductAdapter.notifyDataSetChanged();
                      Snackbar.make(view,&quot;Selected items deleted successfully&quot;,Snackbar.LENGTH_SHORT).show();
                  }
              }
              );
          }
          public static List&lt;Product&gt; getCart() {
              if(cart == null) {
                  cart = new Vector&lt;Product&gt;();
              }
      
              return cart;
          }
      }
  </code></pre>
  <p>When I use the code which is commented out in both the activities i.e use of start activity for result method, this happens:
  When I click on the delete button, items get deleted but the <code>CartActivity</code> closes automatically. The <code>UserActivity</code> with counter text view is shown having '0' value even when there are products in the cart.</p>
  <p>Do tell me about any other info you need from the code. Any other ways I can implement to update the cart counter on pressing of back button after deletion of some items in <code>CartActivity</code> are welcome. Any help is appreciated.</p>