Issue
I am trying to implement a method where my application will track when a user has open or close my application by updating a mongodb database. I understand that when an activity is starting the onCreate() or onResume() methods are always starting and when an activity is closing the onPause() or onStop() methods are calling.
What i' ve tried so far is this: In every activity of my application i am calling this AsyncTask:
public void updateOnline(String fbid, boolean login){
new updateOnlineAsync(fbid, login).execute();
}
public class updateOnlineAsync extends AsyncTask<Void, Void, Void>{
String fbid;
boolean login;
public updateOnlineAsync(String fbid, boolean login){
this.fbid=fbid;
this.login=login;
}
@Override
protected Void doInBackground(Void... params) {
BasicDBObject query = new BasicDBObject();
query.put("fbid", this.fbid);
Document myDoc = fb_users.find(query).first();
if(login){
Log.d("...", "x");
Document listItem = new Document("online", "0");
Document updateQuery = new Document("$set", listItem);
fb_users.updateOne(myDoc, updateQuery);
}else{
Log.d("...", "y");
Document listItem = new Document("online", "1");
Document updateQuery = new Document("$set", listItem);
fb_users.updateOne(myDoc, updateQuery);
}
return null;
}
}
On onCreate(), onResume() methods i use:
ServerRequest serverRequest = new ServerRequest(this);
Profile profile = Profile.getCurrentProfile();
serverRequest.updateOnline(profile.getId(), false);
And onPause(), onStop() methods i use:
ServerRequest serverRequest = new ServerRequest(this);
Profile profile = Profile.getCurrentProfile();
serverRequest.updateOnline(profile.getId(), true);
I think that this should be working and update my document with the user online/offline situation but it doesn't. I wonder if this is because of AsyncTask not working when the application is in background or maybe i am doing something wrong. Anyway any help is much appreciated.
EDIT
Log.d() in AsyncTask ptints y but not x. There for the AsyncTask is executing for onCreate() method but not for onStop()
I just tested by changing activity in my app. The online field is updated in my database when i am changing an activity in my application or when i press the middle button of my smartphone (sending it to background). The method is not working only when i am closing it completely
Solution
The short version is that "you should avoid performing CPU-intensive work during onPause(), such as writing to a database". This is from managing the activity lifecycle (which is a recommended read).
However, as you and others have noted, pausing and stopping an app does not stop the background threads. When you close the app completely, onDestroy is called. This does kill everything (as the name implies).
Also, pausing the activity implies that the UI is partially hidden. This can happen when an alert window is displayed on top of it. However, most cases of onPause happen right before onStop. Whether the app is still running or not, "Once your activity is stopped, the system might destroy the instance if it needs to recover system memory."
Note that the recommendation to avoid CPU intensive during onPause does not apply to onStop. Google's example shows writing to storage. It's not specified there but I would imagine it's actually a bad idea to spawn a background thread to do this, as the system may assume the activity is ready for onDestroy once onStop exists.
My original recommendation still holds though. If you create a Service, onDestroy on an activity, would not apply to it.
Answered By - Roy Falk
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.