Skip to content Skip to sidebar Skip to footer

Android Fragment To Fragment Communication : Update Recyclerview Of The Receiverfragment Via Interface

I have two fragments and I am trying to update the recyclerView of the ReceiverFragment via interface. Both fragments have its own adapter (SenderAdapter and ReceiverAdapter). I ha

Solution 1:

I finally found an answer.

The whole problem happened in the MainActivity. I was communicating from the MainActivity to the ReceiverFragment like this :

ReceiverFragmentreceiverFragment=newReceiverFragment();

And sending the message like this:

@OverridepublicvoidgetMessage(String message) {

        receiverFragment.getMessageFromSender(message);

    }

The message was been passed from MainActivity to ReceiverFragment normally, But I was getting a NullPointerException for the recyclerView of the ReceiverFragment.

The problem was solved by using the following way to deliver the message to the ReceiverFragment (answer found in this link https://www.journaldev.com/14207/android-passing-data-between-fragments):

@OverridepublicvoidgetMessage(String message) {

        String tag = "android:switcher:" + R.id.tabs_pager + ":" + 1;
        ReceiverFragment rf = (ReceiverFragment) getSupportFragmentManager().findFragmentByTag(tag);
        rf.getMessageFromSender(message);

    }

The links for the code and the SQLDatabase (group.db and client.db) are still available for download.

For those who are starting in Android like me and interested in the code, please do not forget to change the delivering function in the MainActivity.

Thank you all and good luck!

Post a Comment for "Android Fragment To Fragment Communication : Update Recyclerview Of The Receiverfragment Via Interface"