I am implementing a Shopping Cart Android application and am showing a custom view on the ActionBar which depicts the cart with no of items in it. This is shown on the action bar while browsing the products and while viewing each product (shown in another activity). From the view product activity the user can click Add To Cart. The click will update the custom view by incrementing the number by 1.
The custom view is this :
But now when the user presses back button it goes to the previous activity, and the custom view on the action bar is showing the old view, where the number of items are less. How do I update the custom view of action bar on back button press
My custom view is this :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/actionbar_cart_layout"
android:layout_width="70dp"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/actionbar_cart_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center_vertical"
android:src="@drawable/ic_actionbar_cart"
android:layout_alignParentRight="true"/>
<TextView
android:id="@+id/actionbar_cart_qty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="right|center"
android:layout_marginLeft="19dp"
android:layout_marginTop="12dp"
android:layout_alignLeft="@+id/actionbar_cart_image"
android:text="0"
android:textColor="#000000"
android:textSize="14sp"
android:textStyle="bold" />
</RelativeLayout>
I am handling the addition of it like this
LayoutInflater inflator = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflator.inflate(R.layout.menuitem_cart, null);
getSupportActionBar().setCustomView(view);
On click of add to cart I am updating the view like this
TextView cartQry = (TextView) view.findViewById(R.id.actionbar_cart_qty);
int count = ShoppingCart.getInstance().getTotalItems();
cartQry.setText(Integer.toString(count));
I am using ActionBar Sherlock with min API version at 11
As per the comment above - overrode onResume() Method in Fragment Activity
@Override
protected void onResume() {
TextView cartQry = (TextView) view.findViewById(R.id.actionbar_cart_qty);
int count = ShoppingCart.getInstance().getTotalItems();
cartQry.setText(Integer.toString(count));
super.onResume();
}