D
D
Damir Karimov2019-05-27 22:11:00
Android
Damir Karimov, 2019-05-27 22:11:00

How to use activity or fragment method in BroadcastReceiver?

I am writing a task application (for myself, for a portfolio and maybe for profit).
This application has a functionality that allows you to mark a certain task as "current" and display it on the notification screen. The notification has a button that allows you to complete the task. To process this button, I just created a receiver and registered it in the AndroidManifest.
But, bad luck, in the onReceive method it is impossible to call a method that allows you to complete the task by accessing the storage. I can only call Toasts on the objects I passed through the PendingIntent when I created the notification.
Who knows how to be? I've been wrestling with this for a very long time.
There was an almost working option when I declared a dynamic broadcastreceiver in an activity, but its drawback is that after closing the application completely (removing active applications from the screen), the broadcastreceiver stopped working.
I do not believe that there are no methods for accessing an application using a static broadcastreceiver, because applications can implement functions like "mark as read"

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2019-05-27
@e-14

The receiver and the activity (and even more so the fragment) have completely different, non-overlapping life cycles .
You can do what you want in many different ways. If you need to show the UI and do something in it, you need to create a PendingIntent for the activity, and, accordingly, start it directly.
With the help of the receiver, you can also do it, it's just not clear why your "storage" is tied to your activity. In the receiver directly (either by starting a service from it, or by scheduling a job in WorkManager, it depends), you can reach the "storage" and change the data in it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question