Re-loading Fragment from inside itself
Inside Fragment put these:
private Callbacks mCallbacks;
public interface Callbacks {
//Callback for when button clicked.
public void onButtonClicked();
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// Activities containing this fragment must implement its callbacks
mCallbacks = (Callbacks) activity;
}
In onOptionsItemSelected change to
case R.id.random:
mCallbacks.onButtonClicked();
break;
Make your FragmentActivity to implements YourFragment.Callbacks :
public class YourActivityName extends FragmentActivity implements YourFragment.Callbacks
Inside the FragmentActivity containing the Fragment add this:
@Override
public void onButtonClicked() {
getSupportFragmentManager().beginTransaction()
.replace(R.id.gridView, new Fragment3())
.commit();
}
This can be done with 2 lines, just add them inside your refresh/reload button (Kotlin code)
try_again_button.setOnClickListener {
val ft = fragmentManager!!.beginTransaction()
ft.detach(this).attach(this).commit()
}