I have a progress bar that I change from View.GONE to View.VISIBLE, just before I call a method writeFile().
Something like this:
onButtonPress(){
mProgressBar.setVisibility(View.VISIBLE);
writeFile();
}
The method writeFile() can take a few seconds (lots of data parsing), but requires additional actions taken by the user afterwards, and thus is not performed on a background thread. This is to avoid the user navigating elsewhere within the app, and then being presented with a pop-up, or redirected, for something they thought they were done with.
My problem is that the Progress Bar does not display until the writeFile() method is complete, and then briefly pops up for a split second before the user is redirected elsewhere.
If I comment-out the method call, then the ProgressBar appears immediately and spins indefinitely, so I know the bar is working properly.
How can I ensure the progress bar pops up FIRST, and then the writeFile() method is called after?
Thanks in advance.
Additionally:
I don't have a lot of experience (exactly none!) with asynctasks, and so am not sure if this is the route I need to take. The behaviour I am looking for is the progress bar to pop up when the user presses the button, and remain on the screen until the method is complete. I don't want the user to navigate around the app during this time.
You can try the below code to check if the progressBar will be visible
new Thread(new Runnable() {
public void run() {
writeFile();
}
}).start();
Related
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.
It's a simple scenario:
You start some background, let's say, network operation in a separate thread.
Set your ProgressBar visible.
Go away from your app before network operation completed.
While your app is in background the network operation completes but you never receive a callback (or fired event) because you should unsubscribe your callbacks/event subscriptions to prevent undesired exceptions (you can only modify UI views from a main thread).
You resume the app and see ProgressBar on the screen despite your background operation has already been finished.
I'm curious what is the best way to handle this scenario.
A possible option could be also using an event bus (for example https://github.com/greenrobot/EventBus) and holding a sticky event when the network operation is completed and checking this in onResume of your activity.
My answer is a Service or an IntentService. While your app is in background I assume you hide the progress bar. When the app is in foreground You can bind to the Service's instance, having maybe a method that returns the current progress. If the progress is grater or equal to the max you show the progress again, otherwise you undertake another action
The general pattern for those this type of scenario (can also include data processing, view updates, etc, etc) is:
/*
`controller` is the object with reference to the task
currently being executed.
It can be anything:
a network operation,
a file copy,
an image processing,
an asset loading, etc, etc...
*/
public void onStart() {
super.onStart();
myView.setSomeProperty(controller.getCurrentValue());
controller.subscribe(this);
}
public void onStop() {
controller.unsubscribe(this);
super.onStop();
}
#Override
public void onControllerSubscriptionUpdate(int newValue){
myView.setSomeProperty(newValue);
}
that way, every time the Activity or Fragment is coming to the foreground, your view gets updated with the latest parameter.
I have a custom table layout where each row is a slide-able view. Everything is working fine, except there is a continuous delay between when a user starts sliding a when the view starts moving. Is there a way to reduce that starting delay?
I am building my table layout inside an async task. So at first I was getting an error about needing to use a looper. Since I didn't know how to do that I use runOnUiThread to handle the problem code. So now I am wondering if that's the reason for the slow response to touch. Here is the code
activity.runOnUiThread(new Runnable() {
public void run() {
handler.post(mResizeViews);
}
});
The question may be "What happen when user sliding?"
if you do some heavily task such as inflate layout when user start slide.
So, check what happen when user start sliding
Im a beginner in Android. I would like "freeze" my app (I mean, the user can't do any action) when I must do some operation.
By example, I delete all row of one my SQLiteBase's table..during this process, I would like show a progressbar and the user can't do anything before the end.
I know (a little) the progressbar mechanism and the AsyncTask but I find it's a heavy solution.. there is a better simpler solution/practice ?
In general, freezing is not a recommended thing and you should always try to allow the user to cancel or go back. It's annoying to wait for operations to finish. More info can be read here.
However, sometimes when you must, you must...
In short, this is how you show a ProgressDialog :
ProgressDialog progressDialog = ProgressDialog.show(this, "dialog title",
"dialog message", false);
(the "false" value is so that the user won't be able to cancel it using the back button)
Then, when you need to dismiss it (upon onPostExecute if you use an AsyncTask), you simply call :
progressDialog.dismiss();
I am experimenting with an app I am developping.
When I launch the app, there is currently a 3 second delay before the app UI is usable. During the delay the screen is black, apart from the task bar and, below it, the app title bar.
I was thinking about displaying a splashscreen as a dialog in the main Activity. However, it is only displayed after those 3 seconds, which makes it useless. This means that nearly all of the 3-second delay takes place between the launch and the call to
super.onCreate(savedInstanceState).
Can anyone educate me on what is happening behing the scenes during this delay ? Is there anything I can do to shorten it ?
Try to locate the slow code and put it into a second thread.
new Thread(new Runnable() {
#Override
public void run() {
// slow code goes here.
}
}).start();