Receive Event when Activity in Background Android
patelakshay13890 opened this issue · 8 comments
How can I receive or subscribe multiple Events even though Activity is in background i.e. activity isn't visible ?
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.
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() ??
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?
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 ?
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);
How should I setup Event Bus for triggering multiple events from my Socket Connections every seconds ?
How should I setup Event Bus for triggering multiple events from my Socket Connections every seconds ?
postEvent in your socket callback method
Closing this issue due to inactivity. 💤 Feel free to comment with more details or submit a new issue.