Issue
I start AsyncTask and put Context in constructor of the Task.
On onPostExecute I want to know is my activity active now and was not recreated. How it's better to do?
For this I can:
create randomNumber in
onCreateActivity and then put it inApplicationclass;onPause/onDestroy set randomNumber to 0 and in
onResumerestore randomNumber;in
onPreExecute()get randomNumber and inonPostExecutecompare randomNumber with Application randomNumber.
May be I can use Context for making decision ... ?
Solution
There are many approaches to check if the activity is still there.
I usually create a SkeletonActivity with the following structure:
public class SkeletonActivity extends FragmentActivity {
private boolean paused = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
paused = false;
}
@Override
public void onPause() {
super.onPause();
paused = true;
}
@Override
public void onResume() {
super.onResume();
paused = false;
}
@Override
public void onStart() {
super.onStart();
paused = false;
}
public boolean isPaused() {
return paused;
}
}
Now let all your activities extend this SkeletonActivity. Finally change this Base Class to change the paused flag as you wish (For example update it in onDestroy()
Another way would be to have a Context instance inside your SkeletonActivity:
public class SkeletonActivity extends FragmentActivity {
private Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = this;
}
@Override
protected void onDestroy() {
mContext = null;
super.onDestroy();
}
public boolean isPaused() {
return mContext==null;
}
}
Answered By - Sherif elKhatib
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.