EventBus icon indicating copy to clipboard operation
EventBus copied to clipboard

Receive Event when Activity in Background Android

Open patelakshay13890 opened this issue 4 years ago • 6 comments

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

patelakshay13890 avatar Sep 14 '21 07:09 patelakshay13890

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.

andob avatar Sep 14 '21 07:09 andob

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() ??

patelakshay13890 avatar Sep 14 '21 08:09 patelakshay13890

Android itself can do two things:

  • either terminate activities, in this case onDestroy will be called
  • or either terminate the entire app process, in this case I don't think onDestroy will be called, but since the entire process will be killed, it won't matter. When a process is killed, its memory allocated in RAM is deleted. When the app / process starts again, it will start "clean".

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?

andob avatar Sep 14 '21 08:09 andob

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 ?

patelakshay13890 avatar Sep 14 '21 08:09 patelakshay13890

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);

andob avatar Sep 14 '21 08:09 andob

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

patelakshay13890 avatar Sep 14 '21 12:09 patelakshay13890

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

postEvent in your socket callback method

tomridder avatar Mar 13 '23 02:03 tomridder

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

greenrobot-team avatar Mar 13 '23 11:03 greenrobot-team