title: how can i refresh fragment when back button is pressed?
body: <p>How can I refresh the view of a fragment, when the back button is pressed?</p>
  
  <p>I have tried this in the onResume method of the fragment but it doesn't work.</p>
  
  <p>OK, here is the code</p>
  
  <pre><code>@SuppressWarnings("unused")
  public class RestaurantMenuFragment extends Fragment {
  
      private static final String TAG = "MenuItemsFragment";
      private static final String CATEGORIES_KEY = "categories";
      private static final String SELECTED_CATEGORY_ID_KEY = "category";
      private static final String RESTAURANT_KEY = "restaurant123";
      private static final String RESTAURANT_KCITY = "city";
      private Spinner mCategoriesSpinner;
      private ArrayAdapter&lt;CategoriesResponse.Category&gt; mCategoriesAdapter;
      private ListView mListView;
      private List&lt;MenuItem&gt; mItems;
      private MenuItemsAdapter mItemsAdapter;
      private EmptyLayout mEmptyLayout;
      private Restaurant mRestaurant;
      private int mCategoryId;
      private List&lt;CategoriesResponse.Category&gt; mCategories;
      private RestaurantActivity mActivity;
      private MainApplication mApplication;
      private CategoriesResponse mCategoriesResponse;
      private ActionBar mActionBar;
      private Gson mGson;
      int categ;
      private ObjectGetter mObjectGetter;
  
      public static RestaurantMenuFragment newInstance(Restaurant restaurant) {
          RestaurantMenuFragment fragment = new RestaurantMenuFragment();
          Bundle args = new Bundle();
          args.putString(RESTAURANT_KEY, new Gson().toJson(restaurant));
           String dd=restaurant.city;
  
           Log.i("dd12", dd);
          fragment.setArguments(args);
  
          return fragment;
      }
  
      public RestaurantMenuFragment() {
      }
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          mActivity = (RestaurantActivity) getActivity();
          mApplication = (MainApplication) mActivity.getApplication();
          mActionBar = mActivity.getSupportActionBar();
  
          mGson = new Gson();
  
          mObjectGetter = new ObjectGetter();
          mCategories = new ArrayList&lt;CategoriesResponse.Category&gt;();
          Log.i("mCategories",""+mCategories);
          mItems = new ArrayList&lt;MenuItem&gt;();
          Log.i("12345",""+mItems);
          mItemsAdapter = new MenuItemsAdapter(getActivity(), mItems);    
  
          Bundle args = getArguments();
          if (args != null) {
              mRestaurant = mGson.fromJson(args.getString(RESTAURANT_KEY),
                      Restaurant.class);
          }
  
          if (savedInstanceState != null) {
              mRestaurant = mGson.fromJson(
                      savedInstanceState.getString(RESTAURANT_KEY),
                      Restaurant.class);
              mCategoryId = savedInstanceState.getInt(SELECTED_CATEGORY_ID_KEY);
              mCategoriesResponse = mGson.fromJson(
                      savedInstanceState.getString(CATEGORIES_KEY),
                      CategoriesResponse.class);
          }
  
          assert mRestaurant != null;    
          updateCart();
      }
  
      public void updateCart() {
          View view = mActionBar.getCustomView();
          Button cartButton = (Button) view.findViewById(R.id.cartButton);
  
          int nOfItems = 0;
          if (mApplication.isCartCreated()) {
              nOfItems = mApplication.getCart().getNOfAllItems();
          }
          cartButton.setText(String.format("%d", nOfItems));
          if (nOfItems &gt; 0) {
              cartButton.setEnabled(true);
          } else {
              cartButton.setEnabled(false);
          }
      }
  
      @Override
      public void onSaveInstanceState(Bundle outState) {
          super.onSaveInstanceState(outState);
          Gson gson = new Gson();
  
          outState.putString(RESTAURANT_KEY, gson.toJson(mRestaurant));
          outState.putInt(SELECTED_CATEGORY_ID_KEY, mCategoryId);
          outState.putString(CATEGORIES_KEY, gson.toJson(mCategoriesResponse));
      }   
  
      @Override
      public void onViewCreated(View view, Bundle savedInstanceState) {
          // TODO Auto-generated method stub    
          super.onViewCreated(view, savedInstanceState);
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
         View view = inflater.inflate(R.layout.spinner_list, container, false);
         RestaurantActivity activity = (RestaurantActivity) getActivity();
         String myDataFromActivity = activity.getMyData();
         String myDataFromActivity1 = activity.getMyData1();
         Log.i("myDataFromActivity",myDataFromActivity);
         Log.i("myDataFromActivity1",myDataFromActivity1);
  
         categ=Integer.parseInt(myDataFromActivity1);
  
          mListView = (ListView) view.findViewById(R.id.list122334);
          mListView.setAdapter(mItemsAdapter);
  
          Log.d(TAG,"Querying items url "
                  + Urls.menuItemsQuery(mRestaurant.id,categ));       
  
          mEmptyLayout = EmptyLayout.with(getActivity()).to(mListView)
                  .setEmptyMessage(R.string.categories_empty_message)
                  .showLoading();                 
  
          loadItems();
          return view;
      }
  
      private void loadItems() {
          mEmptyLayout.showLoading();
          mItems.clear();
          mObjectGetter.getJsonObjectOrDialog(mActivity,
                  Urls.menuItemsQuery(mRestaurant.id, categ),
                  ItemsResponse.class,
                  new ObjectGetter.OnFinishedListener&lt;ItemsResponse&gt;() {
                      @Override
                      public void onFinishedLoadingObject(
                              ItemsResponse itemsResponse) {
                          mEmptyLayout.showEmpty();
  
                          if (itemsResponse != null
                                  &amp;&amp; itemsResponse.items != null) {
                              mItems.addAll(itemsResponse.items);
                          }
                          mItemsAdapter.notifyDataSetChanged();
  
                      }
                  });
          }
  
  
      private class MenuItemsAdapter extends ArrayAdapter&lt;MenuItem&gt; {
  
          private static final String TAG = "MenuItemsAdapter";
  
          public MenuItemsAdapter(Context context, List&lt;MenuItem&gt; menuItems) {
              super(context, 0, menuItems);
          }
  
          @Override
          public View getView(int position, View convertView, ViewGroup parent) {
              final MenuItem menuItem = getItem(position);                
  
              View view = convertView;
              final ViewHolder viewHolder;
              LayoutInflater inflater;
              if (convertView == null) {
                  inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                  view = inflater.inflate(R.layout.menu_item, parent, false);
                  viewHolder = new ViewHolder();
  
                  viewHolder.name = (TextView) view.findViewById(R.id.name);
                  viewHolder.description = (TextView) view.findViewById(R.id.description);
                  viewHolder.price = (TextView) view.findViewById(R.id.price);
                  viewHolder.add = (Button) view.findViewById(R.id.add);
                  viewHolder.selectedView = view.findViewById(R.id.selectedView);
                  viewHolder.remove = (Button) view.findViewById(R.id.remove);
                  viewHolder.total = (TextView) view.findViewById(R.id.itemTotal);
                  viewHolder.quantity = (TextView) view.findViewById(R.id.quantity);
  
                  view.setTag(viewHolder);
              } else {
                  viewHolder = (ViewHolder) convertView.getTag();
              }
  
              try
              {               
                  viewHolder.name.setText(menuItem.name);
                  viewHolder.description.setText(menuItem.description);
  
                  viewHolder.price.setText(String.valueOf(menuItem.price));
  
              }catch(NullPointerException e){
                   e.printStackTrace();
              }
  
              viewHolder.add.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {                   
  
                      mApplication.createNewCartIfPossibleAndAskIfNot(
                              getActivity(), mRestaurant,
                              new MainApplication.OnCreateCartListener() {
                                  @Override
                                  public void onCreateCart(Cart cart) {
                                      cart.addOne(menuItem);
                                      updateItemFromCart(menuItem, viewHolder);
                                      updateCart();
                                  }
                              });
                  }
              });
  
              viewHolder.remove.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
  
                      if (!mApplication.isCartCreated()) {
                          return;
                      }
                      mApplication.getCart().removeOne(menuItem);
                      updateItemFromCart(menuItem, viewHolder);
                      updateCart();
                  }
              });   
  
              return view;
          }
  
          private void updateItemFromCart(MenuItem menuItem, ViewHolder viewHolder) {    
              if (!mApplication.isCartCreated()) {
                  return;
              }
              int quantity = mApplication.getCart().getNOfItemsOfType(menuItem);
              if (quantity &gt; 0) {
                  viewHolder.selectedView.setVisibility(View.VISIBLE);
              } else {
                  viewHolder.selectedView.setVisibility(View.GONE);
              }
              viewHolder.quantity.setText(String.valueOf(quantity));
  
              viewHolder.total.setText(String.valueOf(quantity
                      * menuItem.price));
          }
  
          class ViewHolder {
              TextView name;
              TextView description;
              TextView price;
              Button add;
              View selectedView;
              Button remove;
              TextView total;
              TextView quantity;              
          }    
      }
  
      @Override
      public void onResume() {
          super.onResume();
  
          updateCart();
          mItems.clear();
          if (mItemsAdapter != null) {
  
              mItemsAdapter.notifyDataSetChanged();               
          }   
      }
  
      @Override
      public void onDestroy() {
          if (mObjectGetter != null) {
              mObjectGetter.stopRequests();
          }
          super.onDestroy();      
      }
  }
  </code></pre>
  
  <p>Now, i want to update the listvieww data  when the user pressed the back button. I set the new loadItems() method in the onResume() Method of the Fragment. This Method is called but the old listview data appears and new data also appears...</p>