1
  1. Activity with recycerView set on it, and activity starts fragment to insert new data.

    getSupportFragmentManager()
    .beginTransaction()
    .setCustomAnimations(R.anim.dialog_drop, R.anim.dialog_rise_up)
    .replace(R.id.frame_layout, dropDownDialogue)
    .addToBackStack(null)
    .commit();`
    
  2. Fragment has a save button that ends the fragment as below:

    save.setOnClickListener(new View.OnClickListener() {
         @Override
         public void Onclick(View v){
         //extract data from user.
         adapter.notifyDataSetChanged();
         getActivity().getSupportFragmentManager().popBackStackImmediate();
        }
    });
    

    the problem is recyclerView not updating until relaunching Activity, although onResume, and onStart exists with adapter.notifyDataSetChanged();

Devil10
  • 1,853
  • 1
  • 18
  • 22

2 Answers2

2

https://developer.android.com/guide/components/activities/activity-lifecycle

If you are using a Fragment, it should be local to some Activity (hopefully the one which holds your RecyclerView). From inside that Fragment you can access the Activity containing it with getActivity(), along with any of its public members and methods.

Please see this SO question for more info.

  • You can therefore create a method in your `Activity` called `getRecViewAdapter()` and from your `Fragment` call: `getActivity().getRecViewAdapter().notifyDataSetChanged()` – Alex Feaser Jan 14 '19 at 15:51
0

You can use BroadcastReciever to receive notification when save button is pressed. For example register it inside your Activity:

private void registerReciver() {
    BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
                // broadcast receiver is fired reload your data here
        }
    };
    IntentFilter intentFilter = new IntentFilter("action");
    this.registerReceiver(receiver, intentFilter);
}

Call registerReciver(); in your onCreate() method, now you can launch it from save button:

                        Intent intent = new Intent();
                        intent.setAction("action");
                        getActivity().sendBroadcast(intent);

After pressing save button onReceive should be called. Now you can fetch data again and reload. Even using Intent you can pass data to Activity if needed.

Yupi
  • 4,402
  • 3
  • 18
  • 37