Strange effect in displaying an AlertDialog inside AsyncTask: if the application is minimized during the execution of the AsyncTask:
private class CheckDeviceConfiguration extends AsyncTask<Void, Void, Boolean> {
private ProgressDialog progressDialog;
#Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = ProgressDialog.show(ActivitySIPCountrySelection.this, "Title", "working...", true);
}
#Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
progressDialog.dismiss(); //hide progress dialog previously shown
if (!result) {
AlertDialog.Builder dialog = new AlertDialog.Builder(ActivitySIPCountrySelection.this);
dialog.setCancelable(false);
dialog.setMessage("Message");
dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int arg1) {
//do something
}
});
dialog.show();
}
}
#Override
protected Boolean doInBackground(Void... params)
Thread.sleep(5000);
return false;
}
}
If I click on my app icon to restore, the UI is not responding and the activity looks a bit darkened (inactive?). Back button has no effect.
Edit:
Someone asks where I call AsyncTask. Well, from the Activity onCreate().
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sip_country_selection);
new CheckDeviceConfiguration().execute();
}
The async task shows correctly a progress dialog and hide it in onPostExecute.
NOTE: AsyncTask manages a thread pool, created with ThreadPoolExecutor. It will have from 5 to 128 threads. If there are more than 5 threads, those extra threads will stick around for at most 10 seconds before being removed. (note: these figures are for the presently-visible open source code and vary by Android release).
Leave the AsyncTask threads alone, please.
Pressing the Home switches you from the app to the home screen, whilst leaving your app running in the background.
When your phone is running low on resources like memory it will start to close apps that are running in the background, so that your phone has enough resources for what you're trying to do now. Games are often amongst the first apps the phone will "kill" to save resources as they often use a lot more memory and CPU than other apps. This is why sometimes your game is still running paused, and sometimes Android has closed it for you.
http://developer.android.com/guide/components/tasks-and-back-stack.html. For more details check tasks and back task.
You can cancel asynctask by calling cancel(true), an interrupt will be sent to the background thread, which may help interruptible tasks. Otherwise, you should simply make sure to check isCancelled() regularly in your doInBackground() method.
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
d.cancel(true);
if(d.isCancelled())
{
System.out.println("Destroyed....");
}
}
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
d.cancel(true);
if(d.isCancelled())
{
System.out.println("Destroyed....");
}
}
SO when your activity resumes create a new instance of asynctask and execute again.
http://code.google.com/p/shelves/. Check shelves project by Romain Guy.
http://developer.android.com/reference/android/content/AsyncTaskLoader.html. Also check asynctask loader.
An alternative to asynctask is RoboSpice.
https://github.com/octo-online/robospice.
FAQ's https://github.com/octo-online/robospice/wiki/Advanced-RoboSpice-Usages-and-FAQ.
Related
I need to process some data when the user click the button in one activity, so the screen looks like the app stops for 2-3 seconds. It isn't a lot but I want to give the user information that everything is ok and IMO the best way will be the progressbar which is visible only when data are processed.
I found the code of ProgressBar and it looks like this:
<ProgressBar
android:id="#+id/loadingdata_progress"
style="?android:attr/progressBarStyle"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignBottom="#+id/fin2_note"
android:layout_centerHorizontal="true"
android:indeterminate="true"
android:visibility="invisible" />
and inserted it on the middle of my layout.
And to try if the progressbar works, I put this code
loadingimage= (ProgressBar) findViewById(R.id.loadingdata_progress);
loadingimage.setVisibility(View.VISIBLE);
into onCreate method and everything looks fine.
Then I recreated the code to show this progressbar only if the data is processed.
After click the user invoke this method
public void fin2_clickOnFinalization(View v)
{
loadingimage= (ProgressBar) findViewById(R.id.loadingdata_progress);
loadingimage.setVisibility(View.VISIBLE);
// code where data is processing
loadingimage.setVisibility(View.INVISIBLE);
}
and nothing appear on the screen. I don't know where is the mistake. If I found the progress bar by id, It's strange for me that I can control it in onCreate method but in onclick method it's out of my control.
Your UI thread cannot show progress bar cause it is busy due to your data processing. Try to use this kind of code :
public void fin2_clickOnFinalization(View v) {
new YourAsyncTask().execute();
}
private class YourAsyncTask extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... args) {
// code where data is processing
return null;
}
#Override
protected void onPostExecute(Void result) {
loadingimage.setVisibility(View.INVISIBLE);
super.onPostExecute(result);
}
#Override
protected void onPreExecute() {
super.onPreExecute();
loadingimage.setVisibility(View.VISIBLE);
}
}
EDIT:
AsyncTask let you run code in separate thread and make app more responsive, just put time-consuming code inside doInBackground.
You're not giving the UI time to refresh. Your "data processing" code is running on the UI thread, blocking any visible changes. By the time the system gets control to refresh the display, you've already set it back to invisible.
To fix this, move your processing code to a separate thread or AsyncTask. Then you can set the progress bar to visible, start the task, and have it turn itself invisible once it's done.
I'd recommend AsyncTask for this purpose about 90% of the time on Android, since it comes stock with useful callbacks. The developer guide for it(in the Javadoc linked above) is pretty explicit, and outlines all the steps you need to take.
AsyncTask is too heavily-weighted for such task.
A better much solution
Handler handler = new Handler(getMainLooper());
handler.post(new Runnable() {
#Override
public void run() {
loadingimage.setVisibility(View.VISIBLE);
}
});
Or even simpler (does essentially the same thing as solution above)
runOnUiThread(new Runnable() {
#Override
public void run() {
loadingimage.setVisibility(View.VISIBLE);
}
});
You can try to create a global ProgressDialog not in the layout but in your activity like:
public class MyActivity {
ProgressDialog progress = null;
protected void onCreate(...) {
progressDialog = new ProgressDialog(this);
progressDialog.setCancelable(false);
progressDialog.setTitle("Progress");
}
public void fin2_clickOnFinalization(View v)
{
progress.show();
// code where data is processing
progress.dismiss();
}
}
Hope i it helps
I have an AsyncTask which does a lot of JSON calculations.
public class InitializationTask extends AsyncTask<Void, Void, InitializationResult> {
private ProcessController processController = new ProcessController();
private ProgressDialog progressDialog;
private MainActivity mainActivity;
public InitializationTask(MainActivity mainActivity) {
this.mainActivity = mainActivity;
}
#Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(mainActivity);
progressDialog.setMessage("Die Daten werden aufbereitet.\nBitte warten...");
progressDialog.setIndeterminate(true); //means that the "loading amount" is not measured.
progressDialog.setCancelable(false);
progressDialog.show();
};
#Override
protected InitializationResult doInBackground(Void... params) {
return processController.initializeData();
}
#Override
protected void onPostExecute(InitializationResult result) {
super.onPostExecute(result);
progressDialog.dismiss();
if (result.isValid()) {
mainActivity.finalizeSetup();
}
else {
AlertDialog.Builder dialog = new AlertDialog.Builder(mainActivity);
dialog.setTitle("Initialisierungsfehler");
dialog.setMessage(result.getReason());
dialog.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
mainActivity.finish();
}
});
dialog.show();
}
}
}
processController.initializeData() runs for about 20 seconds. All this works. It even works when I install the application, and pressing home button while application is initializing. The AsyncTask is working in the background. When I restart the application from Android device again after the AsyncTask has been finished, the application still works.
But the application cannot handle this use case: When I deploy the application (or start it when no data is initialized), so that it really takes about 20sec to initialize the data and when I hit home to close the application while initialization (the AsyncTask) runs in the background and start the application again, it leads to unexpected behavior as RuntimExceptions and so on. It seems that the device wants to load the application twice, but none of them can start successfully. How to deal with that?
I thought about checking if there is a running AsyncTask in MainActivity to avoid starting it again:
private InitializationTask initializationTask = new InitializationTask(this);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if (!AsyncTask.Status.RUNNING.equals(initializationTask.getStatus())) {
initializationTask.execute((Void[])null);
}
initializeMap();
}
Unfortunately this does do nothing. Moreover I'm not sure if such use case is possible at all, because when I start the same application twice, they cannot "share" an AsyncTask. Should I somehow avoid starting the application twice or something? I'm not sure what options do I have on this? Any ideas?
I usually do it a bit more bluntly. I set my AsyncTask reference to null when I'm not using it. When the onClick fires, I check if it's not null, which means I started it. If it is null, I create and execute a new AsyncTask right there. It is plenty fast and it's clean enough. One bonus of this approach is that an AsyncTask can only be executed once anyway, so it fits in well with that. At the end of onPostExecute, you can set the reference back to null again if you intend to stay in the same Activity.
While you're perfecting your AsyncTask flow, make sure that it survives orientation changes as well.
I found the solution: It's actually not an AsyncTask issue. The problem was that my parse method of JSONParser (that does the most of the work) which is invoked in processController.initializeData(), was not synchronized.
While I'm updating my database I want to display a progress dialog. My problem is that the ProgressDialog is getting late to appear,after 4-5 seconds, then appears and disappears very fast, it stays on screen few milliseconds almost you can't see it, then new data are shown in the list immediately. This makes me think that the ProgressDialog is waiting for database to be updated(it doesn't take much, about 4,5 seconds) and then it shows on the screen but is dismissing very fast. I would like the ProgressDialog appear immediately I press the 'Update' button and stay on the screen about 4-5 seconds.
class MyAsyncTask extends AsyncTask<Void, Void, Void>{
ProgressDialog myprogsdial;
#Override
protected void onPreExecute(){
myprogsdial = ProgressDialog.show(MyActivity.this, null, "Upgrade", true);
}
#Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
runOnUiThread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
RefreshDataBase();
}
});
return null;
}
#Override
protected void onPostExecute(Void result){
myprogsdial.dismiss();
}
}
When I call it, new MyAsyncTask().execute();
Ok I think that this
runOnUiThread(new Runnable() {
is causing this behavior.
doInBackground() executes your code in a new thread to the main UI thread. You are then putting the code to execute in this thread back into the main one causing the progress dialog to be delayed at the end and then in postExecute() it gets closed immediately.
A good asyntask tutorial can be found here.
You must not use runOnUiThread. What you're basically did is:
Started new non-ui thread
From this new non-ui thread you posted a long running task to UI thread.
Exited from non-ui thread.
Your ui thread now executes long-running operation (RefreshDataBase) and blocks the UI.
You should call RefreshDataBase() directly. And if this method touches UI, you have to refactor it.
I have solved it, using this answer of Vladimir Ivanov.
I have separated the functionality by the appearance.
I have kept the functionality(downloading new data) in doInBackground() and in onPostExecute() I updated the list: get the new adapter,called setListAdaper() and notifyDataSetChanged.
Of course, I quit using runOnUiThread(). Thanks to all for hints.
As I've asken on another question HERE it seems that the PackageManager.getInstalledPackages() doesn't play nice with Threading. As CommonsWare stated HERE:
Perhaps the PackageManager needs to be invoked on the main application
thread, for some reason
So having it on a thread gets undesired behavior, entering and exiting a few times in my Activity makes the list of displayed apps sometimes with items, sometimes empty. Having everything in the UI Thread works like a dream, loads fine everytime. The thing is, the user expects some sort of feedback and I need to provide one. As I start the activity, the screen remains black for 3-4-5-6 seconds (depending on the device and apps installed). How can I provide some sort of feedback ? I am thinking of a ProgressDialog but I don't know how can I start it. Thank you.
As discovered, the loop to work through the applications takes awhile (which can be done in a separate thread), compared to the call to PackageManager.getInstalledPackages() (which has to be done on the UI thread).
Use Async to do background work and show indicator while loading data.
in you onCreate(). call new AsyncCommonDataFetcher(this).execute();
public class AsyncCommonDataFetcher extends AsyncTask<Void, Void, Void> {
Context mContext = null;
private ProgressDialog mProgressIndicator = null;
public AsyncCommonDataFetcher(Context ctx) {
mContext = ctx;
}
protected void onPreExecute() {
mProgressIndicator = ProgressDialog.show(((Activity) mContext), null,
"Please wait", true);
}
#Override
protected Void doInBackground(Void... voids) {
// Do what ever work you like to do. It will do this in backgound and show user a indicator to wait.
}
#Override
protected void onPostExecute(Void voidInstance) {
try {
if (mProgressIndicator != null) {
mProgressIndicator.hide();
mProgressIndicator.dismiss();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
}
Try the following for ProgressDialog in the onCreate() of your activity
this.requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
ProgressDialog LoadingDialog = ProgressDialog.show(this, "", "Loading..", true);
And then dismiss it when the process causing the delay is over
LoadingDialog.dismiss();
I've developed an application that takes content from the internet and shows it accordingly on the device's screen . The program works just fine , a little bit slow . It takes about 3-4 seconds to load and display the content . I would like to put all the code that fetches the content and displays it in a background thread and while the program is doing those functions , I would like to display a progress dialog. Could you help me do this ? I would like especially to learn how to put the code in a background thread.
MY CODE
public class Activity1 extends Activity
{
private ProgressDialog progressDialog;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new AsyncTask<Integer, Integer, Boolean>()
{
ProgressDialog progressDialog;
#Override
protected void onPreExecute()
{
/*
* This is executed on UI thread before doInBackground(). It is
* the perfect place to show the progress dialog.
*/
progressDialog = ProgressDialog.show(Activity1.this, "",
"Loading...");
}
#Override
protected Boolean doInBackground(Integer... params)
{
if (params == null)
{
return false;
}
try
{
/*
* This is run on a background thread, so we can sleep here
* or do whatever we want without blocking UI thread. A more
* advanced use would download chunks of fixed size and call
* publishProgress();
*/
Thread.sleep(params[0]);
// HERE I'VE PUT ALL THE FUNCTIONS THAT WORK FOR ME
}
catch (Exception e)
{
Log.e("tag", e.getMessage());
/*
* The task failed
*/
return false;
}
/*
* The task succeeded
*/
return true;
}
#Override
protected void onPostExecute(Boolean result)
{
progressDialog.dismiss();
/*
* Update here your view objects with content from download. It
* is save to dismiss dialogs, update views, etc., since we are
* working on UI thread.
*/
AlertDialog.Builder b = new AlertDialog.Builder(Activity1.this);
b.setTitle(android.R.string.dialog_alert_title);
if (result)
{
b.setMessage("Download succeeded");
}
else
{
b.setMessage("Download failed");
}
b.setPositiveButton(getString(android.R.string.ok),
new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dlg, int arg1)
{
dlg.dismiss();
}
});
b.create().show();
}
}.execute(2000);
new Thread()
{
#Override
public void run()
{
// dismiss the progressdialog
progressDialog.dismiss();
}
}.start();
}
}
The app crashes , NullPointerException among other stuff . Could you help me ? thanks.
You code is fine, except for the last Thread, which, beside being useless, is the reason your app crashes : when the thread is started, the progressDialog is not initialized yet.
Otherwise, this should work like a charm.
Edit
One more thing: giving null as a onClickListener for the positive or negative button simply dismiss the dialog (which is what you do), so
b.setPositiveButton(android.R.string.ok, null);
is equivalent, only shorter.
You do the downloading in the doInBackground() method. Now you need to override onProgressUpdate() method where you'll do .setProgress() to your progressbar. onProgressUpdate() runs on ui-thread. Use .publishProgress() method from where (from background thread i.e doInBackground() method) you'll make a call to onProgressUpdate().
I hope this idea will help you.
http://developer.android.com/reference/android/os/AsyncTask.html#publishProgress(Progress...)