title: refresh fragment after changing data in database
body: <p>there is a <code>MainActivity</code> with it's <em>fragment</em> named <code>FragmentOne</code>. i have a List of items in this fragment and these data are save in my database</p>
  
  <p>i make changes from <code>NavigationView</code> in <code>MainActivity</code> .. i try to delete all rows from my database. it works, but after that i need to refresh my FragmentOne, how can i refresh the <code>FragmentOne</code> UI ?</p>
  
  <p>i have a method named <code>updateUI</code> in my <code>FragmentOne</code> but how could i access that method? when i make that method <em>public</em> everything just goes wrong!</p>
  
  <p>i tested different ways but failed.. can anybody tell me how can i do this?</p>
  
  <p><strong><code>MainActivity</code></strong> :</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
  private DrawerLayout mDrawerLayout;
  private TabLayout tabLayout;
  private ViewPager viewPager;
  private Detail mDetail;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      //////// TOOLBAR
      Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
      setSupportActionBar(toolbar);
      ActionBar actionBar = getSupportActionBar();
      actionBar.setHomeAsUpIndicator(R.drawable.ic_menu);
      actionBar.setDisplayHomeAsUpEnabled(true);
  
      ///////// DRAWER
      mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
  
      NavigationView navigationView =
              (NavigationView) findViewById(R.id.navigation_view);
      navigationView.setNavigationItemSelectedListener
              (new NavigationView.OnNavigationItemSelectedListener() {
                  @Override
                  public boolean onNavigationItemSelected(MenuItem menuItem) {
                      switch (menuItem.getItemId()) {
                          case R.id.nav_item_one:
                              return false;
  
                          case R.id.nav_item_two:
  
  
                              AlertDialog.Builder alert_delete = new AlertDialog.Builder(
                                      MainActivity.this);
                              alert_delete.setTitle("Alert!");
                              alert_delete.setMessage(R.string.alert_delete_all);
                              alert_delete.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                                  @Override
                                  public void onClick(DialogInterface dialog, int which) {
  
                                      DetailLab.get(getApplicationContext()).deleteAllDetail();
  
                                      FragmentOne fragmentOne = new FragmentOne();
                                      fragmentOne.getFragmentManager().beginTransaction().replace(R.id.card_view, fragmentOne)
                                              .commit();
  
                                              dialog.dismiss();
                                  }
                              });
                              alert_delete.setNegativeButton("No", new DialogInterface.OnClickListener() {
                                  @Override
                                  public void onClick(DialogInterface dialog, int which) {
                                      dialog.dismiss();
                                  }
                              });
                              alert_delete.show();
  
                              return false;
  ..
  ..
                          case R.id.nav_item_nine:
                              finish();
  
                              default:
                                  return false;
                      }
                  }
              });
  
      viewPager = (ViewPager) findViewById(R.id.viewpager);
      setupViewPager(viewPager);
  
      tabLayout = (TabLayout) findViewById(R.id.tablayout);
      tabLayout.setupWithViewPager(viewPager);
      setupTabIcons();
  }
  
  
  private void setupViewPager(ViewPager viewPager) {
      ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
      adapter.addFragment(new FragmentOne(), "ONE");
      adapter.addFragment(new FragmentTwo(), "TWO");
      viewPager.setAdapter(adapter);
  }
  
  class ViewPagerAdapter extends FragmentPagerAdapter {
      private final List&lt;Fragment&gt; mFragmentList = new ArrayList&lt;&gt;();
      private final List&lt;String&gt; mFragmentTitleList = new ArrayList&lt;&gt;();
  
      public ViewPagerAdapter(FragmentManager manager) {
          super(manager);
      }
  
      @Override
      public Fragment getItem(int position) {
          return mFragmentList.get(position);
      }
  
      @Override
      public int getCount() {
          return mFragmentList.size();
      }
  
      public void addFragment(Fragment fragment, String title) {
          mFragmentList.add(fragment);
          mFragmentTitleList.add(title);
      }
  
      @Override
      public CharSequence getPageTitle(int position) {
          return null;
      }
  }
  
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      int id = item.getItemId();
  
      switch (id) {
          case android.R.id.home:
              mDrawerLayout.openDrawer(GravityCompat.START);
              return true;
      }
      return super.onOptionsItemSelected(item);
  }
  
  }
  </code></pre>
  
  <p>here is my method for delete all rows from database in another class called <strong><code>DetailLab</code></strong> :</p>
  
  <pre><code>public void deleteAllDetail() {
          mDatabase.delete(DetailTable.NAME, null, null); }
  </code></pre>
  
  <p>and this is my <strong><code>FragmentOne</code></strong>:</p>
  
  <pre><code>public class FragmentOne extends Fragment {
      private static final String SAVED_NUMBER_VISIBLE = "number";
  
      private RecyclerView mDetailRecyclerView;
      private DetailAdapter mAdapter;
      private boolean mNumberVisible;
      private View view;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setHasOptionsMenu(true);
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          view = inflater.inflate(R.layout.fragment_one_layout,
                  container, false);
  
          mDetailRecyclerView = (RecyclerView)
                  view.findViewById(R.id.detail_recycler_view);
  
          LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
          layoutManager.setReverseLayout(true); //This will reverse the data order but not scroll the RecyclerView to the last item
          layoutManager.setStackFromEnd(true); //For keeping data order same and simply scrolling the RecyclerView to the last item
          mDetailRecyclerView.setLayoutManager(layoutManager);
  
          if (savedInstanceState != null) {
              mNumberVisible =
                      savedInstanceState.getBoolean(SAVED_NUMBER_VISIBLE);
          }
  
          updateUI();
          return view;
      }
  
      @Override
      public void onResume() {
          super.onResume();
          updateUI();
      }
  
      @Override
      public void onSaveInstanceState(Bundle outState) {
          super.onSaveInstanceState(outState);
          outState.putBoolean(SAVED_NUMBER_VISIBLE, mNumberVisible);
      }
  
      @Override
      public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
          super.onCreateOptionsMenu(menu, inflater);
          inflater.inflate(R.menu.fragment_one_layout, menu);
          MenuItem numberItem = menu.findItem(R.id.show_numbers);
          if (mNumberVisible) {
              numberItem.setTitle(R.string.hide_numbers);
          } else {
              numberItem.setTitle(R.string.show_numbers);
          }
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          switch (item.getItemId()) {
  
              case R.id.show_numbers:
                  mNumberVisible = !mNumberVisible;
                  getActivity().invalidateOptionsMenu();
                  updateNumbers();
                  return true;
  
              default:
                  return super.onOptionsItemSelected(item);
          }
      }
  
      private void updateNumbers() {
          DetailLab detailLab = DetailLab.get(getActivity());
          int detailCount = detailLab.getDetails().size();
          String number = getResources().getQuantityString
                  (R.plurals.number_plural, detailCount, detailCount);
  
  
          if (!mNumberVisible) {
              number = null;
          }
  
          AppCompatActivity activity = (AppCompatActivity) getActivity();
          activity.getSupportActionBar().setSubtitle(number);
      }
  
      private void updateUI() {
          DetailLab detailLab = DetailLab.get(getActivity());
          List&lt;Detail&gt; details = detailLab.getDetails();
          if (details.size() == 0) {
              TextView t = (TextView) view.findViewById(R.id.empty);
              t.setText(R.string.empty_view);
              t.setVisibility(View.VISIBLE);
          } else {
              TextView t = (TextView) view.findViewById(R.id.empty);
              t.setVisibility(View.GONE);
          }
  
  //            Toast.makeText(getContext(), "zero", Toast.LENGTH_SHORT).show();
          if (mAdapter == null) {
              mAdapter = new DetailAdapter(details);
              mDetailRecyclerView.setAdapter(mAdapter);
  
          } else {
              mAdapter.setDetails(details);
              mAdapter.notifyDataSetChanged();
          }
  
          updateNumbers();
  
      }
  
      private class DetailHolder extends RecyclerView.ViewHolder
              implements View.OnClickListener, View.OnLongClickListener {
          private TextView mTitleTextView;
          //        private TextView mDateTextView;
          private Detail mDetail;
          private RatingBar mRatingBar;
  
          public DetailHolder(LayoutInflater inflater, ViewGroup parent) {
              super(inflater.inflate(R.layout.list_item_detail,
                      parent, false));
  
              itemView.setOnClickListener(this);
              itemView.setOnLongClickListener(this);
              mTitleTextView = (TextView) itemView.findViewById(R.id.detail_title);
  //            mDateTextView = (TextView) itemView.findViewById(R.id.detail_date);
              mRatingBar = (RatingBar) itemView.findViewById(R.id.ratingBar);
  
          }
  
          public void bind(Detail detail) {
              mDetail = detail;
              mTitleTextView.setText(mDetail.getTitle());
              mRatingBar.setRating(mDetail.getRate());
          }
  
          @Override
          public void onClick(View view) {
              Intent intent = DetailPagerActivity.newIntent(getActivity(),
                      mDetail.getId());
              startActivity(intent);
          }
  
          @Override
          public boolean onLongClick(View v) {
  
              AlertDialog.Builder alert = new AlertDialog.Builder(
                      getActivity());
              alert.setMessage(R.string.alert);
              alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                  @Override
                  public void onClick(DialogInterface dialog, int which) {
                      DetailLab.get(getActivity()).deleteDetail(mDetail);
                      updateUI();
                      updateNumbers();
                      dialog.dismiss();
                  }
              });
              alert.setNegativeButton("No", new DialogInterface.OnClickListener() {
                  @Override
                  public void onClick(DialogInterface dialog, int which) {
                      dialog.dismiss();
                  }
              });
              alert.show();
              return true;
          }
      }
  
      private class DetailAdapter extends RecyclerView.Adapter&lt;DetailHolder&gt; {
          private List&lt;Detail&gt; mDetails;
  
          public DetailAdapter(List&lt;Detail&gt; details) {
              mDetails = details;
          }
  
          @Override
          public DetailHolder onCreateViewHolder(ViewGroup parent, int viewType) {
              LayoutInflater layoutInflater =
                      LayoutInflater.from(getActivity());
  
              return new DetailHolder(layoutInflater, parent);
          }
  
          @Override
          public void onBindViewHolder(DetailHolder holder, int position) {
              Detail detail = mDetails.get(position);
              holder.bind(detail);
          }
  
          @Override
          public int getItemCount() {
              return mDetails.size();
          }
  
          public void setDetails(List&lt;Detail&gt; details) {
              mDetails = details;
          }
      }
  }
  </code></pre>
  
  <p>and here is my layout for <code>FragmentOne</code> :</p>
  
  <pre><code>&lt;RelativeLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:id="@+id/fragment_one_layout"&gt;
  
      &lt;android.support.v7.widget.CardView
      android:id="@+id/card_view"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"&gt;
  
      &lt;android.support.v7.widget.RecyclerView
          android:id="@+id/detail_recycler_view"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"/&gt;
  
      &lt;TextView
          android:id="@+id/empty"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:padding="50dp"
          android:textAlignment="center"
          android:textSize="20sp"/&gt;
  &lt;/android.support.v7.widget.CardView&gt;
  
  &lt;android.support.design.widget.FloatingActionButton
  android:id="@+id/fab"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignParentBottom="true"
  android:layout_alignParentRight="true"
  android:layout_marginRight="24dp"
  android:layout_marginBottom="24dp"
  android:src="@drawable/ic_menu_add"
  android:scaleType="center"
  app:elevation="8dp"
  app:borderWidth="0dp"
      /&gt;
  </code></pre>
  
  <p></p>
  
  <p>my <code>logcat</code>:</p>
  
  <pre><code>  FATAL EXCEPTION: main
  Process: com.drgnme.listhamrah, PID: 23454
  java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.v4.app.FragmentTransaction android.support.v4.app.FragmentManager.beginTransaction()' on a null object reference
  at com.drgnme.listhamrah.MainActivity$1$1.onClick(MainActivity.java:76)
  at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:161)
  at android.os.Handler.dispatchMessage(Handler.java:102)
  at android.os.Looper.loop(Looper.java:148)
  at android.app.ActivityThread.main(ActivityThread.java:5451)
  at java.lang.reflect.Method.invoke(Native Method)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
  </code></pre>