I have a custum listview with image, text, a download button and a progress bar for each item.
The plan is that when I click the download button, the progress bar which original is invisible becomes visible and starts to progress, while the download button changes to a cancel button.
But what happens now is that when I minimize the app to home and opens it back up, the listview refreshes(that is the adapter is called again) as the activity resumes. This undos all the states which have been set.
the progress bar becomes invisible and the download button shows up with the download icon instead of the cancel icon, even though the download continues in Asynctask.
What I need to do is to find a to set the state of the list item, such that the progress bar continues to progress and the button changes to cancel button. i.e to retain the ui state as the download goes on in the Asynctask, even when the listview is reinitialized.
I viewed` your question. The problem is regarding the Asynchronous AsyncTask that you used. I can try to explain you the problem and the recommended solution.
If you start the AsyncTask inside your activity, Now when you close the activity (finish) It is not guaranteed that the running AsyncTask also stop working and finish itself because of your activity is no longer running. That does not happen.
Another fact is that, As previously said that once you fire the AsyncTask you could not reuse it next time when you start the same activity again. In your case, it always starts the new AsyncTask when you open the activity.
Alternative Solution: You can use the Service component that can run in the background (keep in mind that it does not create the separate thread from your application, It's run on the application thread) means it has no UI and could be run even if your activity or application is not in front of the user. You can put your download logic inside it and bind with your running activity.
Regarding the state of the UI in adapter that you can manage by communicating between the activity and running service.
This is simple approach that you can use it.
Hope this will help!
Thanks
Bhavdip
Related
I use MVP and it's work fine.
User click button on the View
show progress
call peresenter.download file
after success download file call view.hideProgress
OK. It's work fine.
But has one problem.
When user click button the progressbar is show. But if I change screen orienation then as result view is recreate and progress bar is hide. But operation was not finish yet. The operation is finish after 10 seconds.
So the question is:
How show progress bar (restore state) while file was not download success?
You can use Moxy library. It helps to save and restore state on configuration changes because it adds ViewState layer.
https://github.com/Arello-Mobile/Moxy
If you prefer native ways you can use loaders or retain fragments
I have one activity that keeps refreshing the elements on it based on the responses from our server, and i created another activity that overlays the 1st activity with some information i need to collect from the user if he wants, but when i pop up this 2nd activity which is transparent and shows what is in the background, the objects that were getting refreshed stopped moving because the activity went to pause mode.
Still i can see the content on the activity that is behind but nothing is moving anymore.
How can i get my 2nd activity working with the transparency and still be able to see the content of the background activity running?
You don't. An activity is paused whenever it is no longer the foreground activity- whenever any activity is launched on top of it. The transparency doesn't matter. If you don't want to be paused, don't make the new functionality an activity- make it a dialog, fragment, or an overlay of some sort.
You cannot. Android only allows one active activity at a time. So once an activity is in foreground, another activity will be placed in background and triggers onPause method. To make a transparent activity, you can make it as a dialog activity.
In an Android app, I am trying to make a ProgressBar visible when a button is clicked, conduct some processes, and then make the bar invisible again. A general overview of my button's onClick method is shown below:
ProgressBar bar = (ProgressBar)findViewById(R.id.prog_bar);
bar.setVisibility(View.VISIBLE);
...do some stuff...
bar.setVisibility(View.INVISIBLE);
However, when I run the code, while the process in the middle is completed, the ProgressBar never shows up. Any ideas?
OnClick is called on the main thread. Your changes to the ProgressBar will not have any visible effect until your OnClick method returns. By that time, you've already finished all your work and hidden it again.
Move your work to a background thread. Not only will this make your app more responsive, it will allow the prograss bar to appear. (Note that, when your work is finished, you'll have to hide the progress bar from the main thread, not the worker thread. Typically you post a Message to a Handler to achieve this.)
How to get an indeterminate progress bar between two activities, I mean, I'm in activity A, show the progress bar, while the progress bar is showing, a new activity B is started and in this activity B I want to close the progress bar. Thank you.
You can't do it that way. An Activity is what holds everything on the screen for your application. If you are "inbetween" two activities then you have no way to control what is on the screen.
You need to put the progress bar into ActivityB, and make it visible by default, then when you are done loading your data make it invisible with
progressBar.setVisibility(View.GONE);
If your ActivityB is taking a long time to load before showing any Views then it is an indication that you are doing some loading work on the Main thread, that you should isntead be doing in the background, and updating the Views once loading is complete.
There is no reason to do so. Starting a new activity is a very fast process, almost instant (unless you onPause method takes very long), so the progress bar will never be shown.
That being said, there is a hacky way to get a progress bar to be shown between activities
you call ProgressActivity(that you create) from ActivityA then after waiting for some time (delayed runnable) you call ActivityB. This does nothing constructive and should not be used.
I have an activity that contains a listview. When another activity shows this activity, everything works fine and I am able to return back to the first activity. But if I then call that same activity again a short while later, that activity will launch and the code does get executed. I can even see the listview's adapter getting called, so everything seems to be working fine. But once the onCreate completes and listview has completed its job of displaying its items, the screen for that activity goes blank. Even worse, if I press the Back button, I cannot return back to the first activity. My app isn't hung because I do have a service running in the background and can verify that it is still running by placing a breakpoint in it. Also, I can see in Eclipse that the app is still running. So what could be causing the screen to go blank and cause the keyboard to freeze up? I can post the code but it is rather involved and am just looking for clues at the moment.