Handling Progress Dialog in asynchronous events - android

In my android application, there is a situation where multiple asynchronous events happen at the same time and need to show a progress bar when such events happen. For example, user clicked a button to download a file from internet. An async task is spawned and a progress dialog (As a dialog fragment) is shown. Lets say user rotated the screen and in the oncreate there is another async task which loads a file from disk in another async task and also needs to a progress dialog. I end up in a situation where there are multiple progress bars appear one over other and gives a bad UI experience. Is there a way to have a shared progress dialog manager kind of implementation in the framework which takes care of these kind if scenarios
Any help is appreciated.

Related

Espresso: can not click on pause button when UI is busy updating progress bar for download

I'm testing an application using Espresso. The scenario is when the user click on the "Download" button, the data (a music/files/images etc.) starts being downloaded from the internet and UI shows a progress bar. User should be able to click on the same button to pause the download. The test code was pretty simple:
// click to start download
1) onView(withId(R.id.actionBtn)).perform(click());
// now either monitor the progress bar or simply click the same btn again to pause the download
2) onView(withId(R.id.actionBtn)).perform(click());
However with Espresso, it seems that the 2) click() is not taken effect until the download is complete. i.e. not possible to "Pause" the download in the middle.
I understand that "By default, Espresso waits for UI operations in the current message queue to process and default AsyncTasks(synchronizes with the default AsyncTask thread pool) to complete before it moves on to the next test operation. ". I assume in this case, the UI is busy downloading and updating progress bar and only until download completes will the UI thread be idle to let the second Espresso operation to be executed ?
However, if I do need to test the "pause", and say, also monitor the progress bar to pause at, say, 20% of download, is there a way to get around it using Espresso ?
I did read something about idling resources but not sure if it could or how to apply here. Any advice is appreciated.

Android Global Progress Dialog

I have an AsyncTask class that handles all networking calls for my app. I also have a Globals class that holds a progress dialog. Now, what I want is for there to only be one progress dialog at a time; this is fairly simple. The Globals class initializes the dialog, and each time it is used by the AsyncTask, only the message is changed. My issue is determining when to dismiss it. If I use p.dismiss() at the end of onPostExecute(), then the progress bar will go away until the next AsyncTask runs, which is not what I want. However, if I don't include the line there, then the progress dialog will stay up indefinitely. I guess what I'm trying to find is a way to see if all AsyncTasks have finished running. By doing this, then that one global progressdialog will be able to close without looking wonky.
Chaining together all the AsyncTasks is not an option (e.g. having the first one execute the second in its onPostExecute()).
Also note that there could be anywhere between one and five AsyncTasks running at the same time.
How can I determine if every AsyncTask is complete?
you could keep track of how many you are starting and create a callback to the class every time a task finishes, then when the returned count matches the start count you know all the tasks are done.
there really is no way to know when see what tasks are running without doing some thing like that.
there is a getStatus but you would have to keep polling for the status to see if the individual task is done or not

on Which Activity background I should display progress dialog when user can navigate to different activity based on his data in server

In my app I am creating Log in mechanism.In this when user enter username and password then in background data is fetch.On the basis on this fetched data user can navigate to different activity.Like if user is registered,if user not registered,if username and password is incorrect.
I have implememnt all this functionality.But here I am displaying progress dialog on the login Activity and then I navigate the user based on condition satisfied.But as the data is large hence activity layout taking more time to load and I have already dismiss the dialog in onPostMethod of AsyncTask.
So for 2-3 sec login activity is standstill without dialog(as it is already dismiss)
Hence where should I display the log in progress dialog.
Please tell me if any other detail is needed.
I would take the route of checking whether or not you will still be using the Dialog UI component based upon the response. And if it involves a task that will take some time to accomplish, I wouldn't dismiss the ProgressDialog at that moment.
I would wait until the operation completes in your follow up Activity and then dismiss it there. Not sure what else we can as a community offer as a solution without any code or direct issues. We can probably just speculate and come up with architectural/structural responses.
Process all data in doInBackground in onPostExecute just navigate to new activity. for checking is user registered or not etc use some flag in doInBackground method.
Dismiss the progressDialog immediatly ofter your startActivity() call.
*Make sure you dismiss it in the UI Thread.
I'm working in mono for android c# and my code will look like this (just change it to java):
var myIntent = new Intent(this, typeof (MyActivity));
StartActivity(myIntent);
_progressDialog.Dismiss();

global indication of activity in android

in my app, i'm uploading some files that can take up to several minutes. i'm thinking of a way to notify the user about activity going on passively by adding a progress bar in my custom title bar. what i want to do is have every activity, each which uses the custom titles, appear with the progress bar until the thread finishes and does a callback which would make invisible the progress bar. can something like this be accomplished?
what seems to make this impossible is that if the user is in an activity with the view loaded, the thread finishing callback would have to manipulate the loaded view resources to disable the progess bar which doesn't seem feasible. are there any suggestions to accomplish this or alternative solutions in keeping a global and passive indication of something going in the background?
You can use a service to achieve this. Services
Basically how it would work, is you bind to the service in each activity when you create the activity. You use this service to start your upload method.
When you bind to the service you pass a handler, which is then used to update your UI in that specific activity. The service will never directly affect the UI (it will be running on a separate thread) instead the handler passes a message back to the UI thread with data in a Bundle, such as upload progress, or a bool to say it's finished.

Display loading screen using AsyncTask with ListActivity

I've got an app that uses ListActivity to give users a list of actions. When they click one I use an Intent to launch a separate activity.
My problem is that the actions that the app performs take about 20 seconds to finish, and since I don't want the user to receive that nasty ANR dialog, I tried to use AsyncTask to present them with a loading screen in the mean time. I tried using setContentView(R.layout.loading); on onPreExecute(), but it throws a NullPointerException which as far as I have figured out is due to the fact that loading.xml is not "a ListView whose ID is android.R.id.list".
So what can I do now? How can I show that loading screen? Is there a way around this pretty annoying situation? Any help would be greatly appreciated. Thanks!
I am not sure exactly what your use case is; you have a list of items that are populated immediately, and upon selecting one an action is taken? The action that is taken is to launch another Activity which performs background processing?
Or does it take that long to populate the list of actions?
If the former, you can use an AsyncTask for the long-running activity instead of an Intent to launch another Activity: in the callback you get for the click on the item in question, you would create the AsyncTask, and in doInBackground you would perform the long-running activity, with onPostExecute refreshing or manipulating your list as necessary.
Another thing to consider is using a dialog box to show a loading screen, if the loading is required to happen before you launch a new Activity.
If you can further describe your use case, I can help you more.
It's not the loading screen you need to have on the AsyncTask, it's that 20-second Activity initialization. I would look for a way to do all the setup in a background thread in a Service while the user is free to merrily bop around in other Activities. I'd try hard to find a way not to just stall the user for 20 seconds. Maybe take them to the target Activity and show them data cached from their last visit until the new set is ready.
Fire up and display your loading dialogs in your onCreate() of the Activity being called, then call Dialog.dismiss() in your AsyncTask's onPostExecute().

Categories

Resources