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
Related
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
this is my first post here so hello everyone and forgive me any mistakes common for newcomers.
In my actual Android 4.0 project im using Options Menu which is opened by clicking on one of ActionBar items. Items of my menu are asynchronously updated so in some cases it opens before changes has been made and this is proper behaviour for me. As we know, after user changes an orientation of the device, whole activity is recreated (same for its menu). This use case is properly handled in my code - state of Activity is saved.
Problem happens when user opens Options Menu and changes orientation when menu is still visible - menu gets recreated and shown. I would like to make it not appear after it has been created.
Is it even possible? I assume that i should do something either in onCreateOptionsMenu() or in onPrepareOptionsMenu() method.
You could use closeOptionsMenu() to close your OptionsMenu manually. If you want to open it at any later time, you could call openOptionsMenu().
I can think of a workaround. set a class variable true in onRestoreInstanceState. override onPrepareOptionsMenu return false if that variable is true and set that variable to false again. It should give you the behavior you want. comment if there's any problem, I'll post the code.
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.
The Title is almost the entire question but i'll complement it with some things :
-(1) I have a AsyncTask for get some data from Internet
-(2) I have a AsyncTask for display a Progress Dialog
Before call (1) , I execute (2) dialog.show() and when task (2) ends I call dialog.dimiss(). All is doing right , but while the Progress Bar is showing the Menu Button stay unresponsiveness, ie , nothing happens...
I would like to know if it is the default behavior or i missing something ?
I'm looking for it and did found anything that clear me about it..
Aprecciate any advice
You mean to say that while the dialog is showing, pressing the hard menu button does not bring up the menu. Did I get it right?
If so, then I see the same behavior as you. But according to this:
For example, when a dialog is open,
the Menu key reveals the options menu
defined for the Activity and the
volume keys modify the audio stream
used by the Activity.
So I would expect that the menu button should still work even if the dialog is showing, but based on my experience, it does not.
After the dialog is dismissed, the menu button should work again.
onCreateOptionsMenu is meant to prepare the dialog. Once it is shown and in use it is no longer being prepared and thus use of the dialog is then handled in onOptionsItemSelected.
I have an activity here.
I want to click a button and then hide the activity GUI.
That is, GUI is needed and you can hide it by clicking a "Hide App" button. How can i
implement this "Hide App"?
Somebody help! Thanks in advance!
To do what you want within the organizational model of android, your "program" should be written as a service, not an activity. You would then have a gui that is an activity and a client of your service, which can be started (made visible) and paused/stopped (hidden) as desired.
Presumably when your user clicks the hide application button, you're going to want to show something - at the very least a show button, so the user isn't stuck without input options!
So what you really have then is two views, one with the GUI hidden.
Two approaches I can see:
Hide app calls another activity with only the UI shown that you want. When the activity is finished, use Activity.finish() to return to the original activity with the GUI
Look at ViewAnimator and its subclasses (ViewFlipper and ViewSwitcher)
You could also just enable the screen lock. ;-)
That would automatically lock the screen (hide your app). And when the user unlocked the screen (using the UI and a gesture the user is already very familiar with) he would automatically get back into your app without you needing to do any extra coding.
The additional advantage of the screen lock is that it can be be password-protected, so if the user has his screen-lock already set to a password, instead of a slide bar -- he would just get the slide password thingy.