Solution 1 :
You can’t reliably determine if an Activity
is active. What you should do is to store the data somewhere (file, SQLite database, etc.), and then send a broadcast Intent
that means “new data is available”. Your Activity
can register a listener that will get triggered by that broadcast Intent
if the Activity
is alive when the broadcast Intent
is sent. It can then pick up the data from wherever you put it and do whatever you want with it.
Solution 2 :
There is a simple method but it doesn’t require JSON data. you can just add data as public static data.
public static int GRID_COUNT = 2;
If you are using that data for reading purposes, you can do it like this.
public static final int GRID_COUNT = 2;
Problem :
I have a service called EventReceivingSevice
which will get new data in onDataRefresh(JSONObject data)
function.
private void onNewData(JSONData data) {
boolean isActive=isActivityActive(); //Check if activity is active
if(isACtive)
passData(data);
else
storeData(data);
}
An activity called HomeActivity
will display the data. When EventReceivingService
will get new data, it has to be checked if HomeActivity
is active, it has to pass the data to HomeActivity
, and if not it will store the data somewhere so that HomeActivity
will later use that.
The data is in JSON format.
How can achieve this?
Comments
Comment posted by Lifecycle Aware
You can use the
Comment posted by talentica.com/blogs/android-life-cycle-aware-components
@tomerpacific Do I need to use android.arch (
Comment posted by Link
I don’t know why you went to a tutorial instead of the documentation link I sent. In the documentation itself, there is a link that shows how to add the android.lifecycle dependency.
Comment posted by TrueCoder
@tomerpacific Can you please provide an implementation of Lifecycle Aware?
Comment posted by David Wasser
How large is this data?
Comment posted by TrueCoder
But, what about storing data? How will I get to that when I have to store data in file?
Comment posted by question
check this