greenrobot / EventBus

Event bus for Android and Java that simplifies communication between Activities, Fragments, Threads, Services, etc. Less code, better quality.
http://greenrobot.org/eventbus/
Apache License 2.0
24.68k stars 4.66k forks source link

Receive Event when Activity in Background Android #673

Closed patelakshay13890 closed 1 year ago

patelakshay13890 commented 3 years ago

How can I receive or subscribe multiple Events even though Activity is in background i.e. activity isn't visible ?

andob commented 3 years ago

simply call register() inside onCreate then unregister inside onDestroy.

I usually do it like this:

EventBuses.kt file

val ForegroundEventBus = EventBus()
val BackgroundEventBus = EventBus()
abstract class BaseActivity() : AppCompatActivity
{
    fun onCreate(savedInstanceState : Bundle?)
    {
        super.onCreate(savedInstanceState)
        try { BackgroundEventBus.register(this) }
        catch (ignored : Exception) {}
        try { ForegroundEventBus.register(this) }
        catch (ignored : Exception) {}
        //wiil thow exception if activity doesn't have a method annotated with Subscribe, so I'll just ignore the exception
    }

    fun onPause()
    {
        ForegroundEventBus.unregister(this)
        super.onPause()
    }

    fun onDestroy()
    {
        BackgroundEventBus.unregister(this)
        super.onDestroy()
    }
}

Then I extend all my activities from BaseActivity. Usually I put more configuration stuff (other libraries etc) inside BaseActivity.

Then if I want an event to be received only by foreground activity, I do ForegroundEventBus.post(someEvent) Or if I want an event to be reveiced by all background activities, I do BackgroundEventBus.post(someEvent)

Please close this issue since it's not a bug related to the library functionality.

patelakshay13890 commented 3 years ago

Can you please provide example for the same it will be most helpful ? And what happened if User Clear App from Recent List than What happened to BackgroundEventBus as there will be not call for onDestroy() ??

andob commented 3 years ago

Android itself can do two things:

As far as I know, when use clears the app from recents, the process gets killed.

I don't understand the first question. The example I gave is self explainatory?

patelakshay13890 commented 3 years ago

I am using java right now so please provide EventBuses.kt file in java and I don't get this how to declare and use ?

andob commented 3 years ago

ok

public class EventBuses
{
    public static final EventBus ForegroundEventBus = new EventBus();
    public static final EventBus BackgroundEventBus = new EventBus();
}
import static your.package.name.EventBuses.*;

public abstract class BaseActivity extends AppCompatActivity
{
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        try { BackgroundEventBus.register(this); }
        catch (ignored : Exception) {}
        try { ForegroundEventBus.register(this); }
        catch (ignored : Exception) {}
    }

    public void onPause()
    {
        ForegroundEventBus.unregister(this);
        super.onPause();
    }

    public void onDestroy()
    {
        BackgroundEventBus.unregister(this);
        super.onDestroy();
    }
}

Then, to use:

AnyFile.java

import static your.package.name.EventBuses.*;

ForegroundEventBus.post(whatever);
BackgroundEventBus.post(whatever);
patelakshay13890 commented 3 years ago

How should I setup Event Bus for triggering multiple events from my Socket Connections every seconds ?

tomridder commented 1 year ago

How should I setup Event Bus for triggering multiple events from my Socket Connections every seconds ?

postEvent in your socket callback method

greenrobot-team commented 1 year ago

Closing this issue due to inactivity. :zzz: Feel free to comment with more details or submit a new issue.