I would like to run an async task when the app is closed out and only then. I tried putting it in the onDestroy() method but it only gets called sometimes (such as when the app is crashed). I also tried to put it on the onPause() method, but it also runs when the user tries to look at a different app rather than only when the app closes. Thanks!
Sincerely, Rocky
You should use Thread instead of AsyncTask, but if you want to use AsyncTask you'll need to do the following:
Don't update the UI in AsyncTask because onDestroy removes the resources and context for that activity. It will crash if you try to update them.
Use the application Context not the activity Context when you create your AsyncTask, because the activity Context is invalid after the activity is destroyed.
Related
I have read that an AsyncTask keeps running even if the calling activity has stopped. Keeping this in mind, I am trying to understand the working of onTaskCompleted. onTaskCompleted is usually called from onPostExecute() of the AsyncTask and if the calling activity still exists, everything is good. But what if the calling activity has stopped? Will this result in a crash of the app?
This question came to me because I was thinking of updating some global variables or UI components in the calling activity on the result of the AsyncTask. Is this a good idea? Won't this also result in a crash?
Thanks.
I have a complete button in page, when user click button, it will start an async task and pass the activity context into it, then finish the activity to dismiss the activity view.
What will happen if the activity be finished but the background task still need to use the context reference passed in? will it have any concerns? or have alternative/better way for this kind of situation.
any suggestions will be appreciated!
First of all, in this situation you better go for service instead of asynctask. In your case, Service should stop itself once task is finished.
Even if you go for asynctask, try to use application context that you can get from activity context as below:
activity_context.getApplicationContext();
The application context will be there even if activity is finished and also it will avoid memory leak.
And if you want to make the asynctask run safely even if activity is finished, then try not to update any UI in postExecute(..) method as it will run on the UI of the activity which is already finished leading to exception. Try to do only any background task inside doInBackground(...) which runs on different thread.
Hope this answers your question.
i'm fairly new to android programming. I have a main activity that gets data from a DB through a service handler (url). I want to insert data as well, but on a different activity, and i want my main activity to be up to date each time its been called (onresume(),onrestart()).
I've found this on the Android API reference about AsyncTask:
The task can be executed only once (an exception will be thrown if a second execution is attempted.)
Does that mean that i cannot call the AsyncTask whenever the activity resumes, or that i cannot have multiple "instances" of the AsyncTask running at the same time?
It literally means, that while AsyncTask is running, you cannot launch it again.
In your MainActivity.class you have line:
task.execute();
if task either is finished or not and you call the method again then exception will be thrown.
And put this method in onResume() is a good practice.
Only one thing to notice is: if you put in onRestart() remember, this callback works when you change configuration, but it will not be called if you Create activity.
The doc about lifecycle of an Activity.
Does that mean that i cannot call the AsyncTask whenever the activity resumes, or that i cannot have multiple "instances" of the AsyncTask running at the same time?
It means you cannot call the execute method twice on the same AsyncTask instance regardless it is completed or not.
I have an application with several activities, and I have a timer I start in the first activity that is presented. The problem is this:
How can I get a reference to the current activity when the timer goes off when the activity I'm currently may not be the same as the one I started the timer.
What I actually want is to have a timer traverse all my actives, show an alert dialog when it expires and the do some stuff. But because of the way android works this seems to be impossible. Does anyone has an alternative?
I've already tried:
Using an async task to access the ui thread, doesn'nt work if it is not created in the main ui thread.
Can't use a Handler, my timer is in another class
What other choice do I have?
EDIT:
I canĀ“t change any of the activities code, the timer should be decoupled enough to function when someone plugs it in the project.
Getting an instance of the current activity from the timer worker thread should work, since it would let me run stuff in the ui thread.
Implement your timer as a singleton.
Then, implement an observer pattern:
Create an interface (maybe called AlertListener) that is implemented by each Activity you want to be alerted. This interface should have a method, something like onTimerExpired(). This method should do whatever needs to be done to the activity when the timer expires.
In your timer class, also maintain a reference to the current AlertListener and a method, named something like "setCurrentActivity(AlertListener currentActivity)".
In onResume or some other method of each activity, call MyTimer.setCurrentActivity(this).
When the timer goes off, call currentActivity.onTimerExpired().
I need to implement auto-login process from an activity. Also I need to show ProgressDialog while logging in. I use AsyncTask for login process, because I haven't succeeded to show ProgressDialog in other way, and I execute the mentioned AsyncTask in onCreate. I read that all the code which is executed after doInBackground must be written in onPostExecute, but I have code in onResume. The onResume is launched when AsyncTask hasn't finished its execution yet. Is it possible to launch onResume only after AsyncTask is finished? Or is it possible to execute other functions after AsyncTask is finished (in general)?
Is it possible to launch onResume only after AsyncTask is finished?
No, you need to follow the Activity Lifecycle. And the AsyncTask is asynchronous so that it can run while your UI can still do other things. What you can do is put the code that is in onResume() that you want to run after the task has finished in your onPostExecute() method. There are other options but without seeing the code you are trying to run, this would be my suggestion.
Or is it possible to execute other functions after AsyncTask is finished (in general)?
Yes. You can put that code in another Activity method and call that from onPostExecute()
You also can use getStatus() method of AsyncTask to run code based on the Status of your AsyncTask
Short Answer: No.
If you check the developer android website or this image, you'll check that OnResume() after the Activity is loaded, either after created, paused or restarted. In my case, I have that verification done in OnResume().