There were problems when updating the list view from the broadcast receiver .

There is a listView on the fragment, there is a ConnectionChangeReceiver .

It is necessary, when changing the connection of the device (Internet access), to show something like "No access to the Internet" in LV , or, conversely, to remove this inscription.

The problem is that I updated LV , for example like this:

JournalFragment journalFragment = (JournalFragment) ((MainActivity) context).getSupportFragmentManager().findFragmentByTag(JournalFragment.TAG); (journalFragment).updateLV(); 

but I can’t get the MainActivity from the onReceive method, there is a context , of course, but I cannot imagine it as a (MainActivity) context .

What to do ?

    1 answer 1

    It is enough to describe the class extending receiver, inside the class in which there is a list update method. Then from this internal class it will be possible to pull the methods, the class in which it is described.

    -UPD- code to register the receiver

     LocalBroadcastManager .getInstance(getApplicationContext()).registerReceiver(new Имя_Ресивера(), IntentFilter(Тут заголовок на который будет реагировать ресивер)) 

    Decided to add another message sending for full transparency

     LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(new Intent(Тут заголовок на который будет реагировать ресивер)) 
    • if I understood correctly, then I did the following: inside the class I described a new receiver, inherited from the first one, in it I redefined onReceive, and called the list update method. If everything is correct, it does not work) should it be registered somehow or should it be announced somewhere as a receiver? ps the first (main) receiver is already announced in the android manifest. - Alexander Popov
    • Of course you need to register it. In the update question added the code to register the receiver. - Kota1921