I am working on a project that lists items that users can click on any of them and see its detail in detailsActivity and if they like it so the can download it! i am using asynctask to list all the items from server in the doBackgournd method and display the result in onPostExecute method. The problem is when users start a download and go back to the list and click on another item the detail of the item is displayed only when the first download finishes!
I want the users be able to choose any item and go the detailsActivity while the download is running for other items they previously started.
please help
You should try and place your download to a Service.
A Service is an application component that can perform long-running operations in the background and does not provide a user interface.
AsyncTask in (earlier versions after android 3.0 I think) android works sequentialy by default
if you want to change this behavior you can use executeOnExcuter instead of execute
for example
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
Related
Currently I am implementing MVP pattern for one of my module which has multiple loaders but I am facing problems in updating the Notification from the second loader call
Scenario:
Fragment invokes Presenter.loadMethod() initially to load the data in gridview and once the user clicks on individual item Presenter invokes Presenter.Download() method to download the file(not using download manager)
And I need to update the progress indication like whether the file is in download state or progress state but when the orientation is changed I am not able to update the notification bar when I use InitLoader().
I tried with restartLoader() too but still facing the same problem.
Can anyone please explain what is the best approach to solve this problem?
You need to keep list of items being downloaded, save the list in onSaveInstanceState(). Start a separate loader for every download item.
In onCreate() method, you restore the items being downloaded and reconnect to loaders by calling getLoaderManager().initLoader(itemId, null, this).
In onLoadFinished(), you should check if an item is downloaded successfully and change its state to "downloaded", then remove the item from the list.
I've got the following problem. In my app I'm loading data in an AsyncTask. The problem is, when the user now clicks on the icon to open the Navigation Drawer and opens up another fragment the app crashes. When the AsyncTask is finished the app doesn't crash. The problem that is encountering is, that when I switch the fragment (The fragments are always the same, just with another content dependent on the NavigationDrawer Item click) the app crashes.
I guess the problem is, that the async task isn't finished, I'm calling the same fragment again want to display different data.
So what would be my approach to handle this? Use for every different view a different fragment? I thought using the same fragment every time is much easier, since it's just displaying different data but the structure, layout etc. is all the same. Just the data that it gets is different.
I also thought about somehow "blocking" the user from doing any other actions while the asynctask but still show him that the app is processing. But that would be not the definition of an AsyncTask.
How would you approach it? Use different fragments for every different display? Or how? Block somehow? If a user clicks on an item of the navigation drawer the asynctask needs to stop all its actions (if some are done) and then restart doing all the actions. Is there a way to do it?
Please note that the fragment where the async is executed and the activity where the fragments are called are in two different files
You can either block the screen with a loading screen (not that good UX wise...) or you could cancel the asynctask when you change the fragment, in the destroy or detach method.
You didnt show the errors, but I would guess that the app crashes because you are trying to acess something in the asynctask onPostExecute method and it is no longer available...
I guess that it crashes because your AsyncTask is sending data to a class instance that doens't exist.You should change the Class that receives callbacks from asynctask. Anyway i can't give you a better answer till i will see your real code of AsyncTask ( at least onPostExecute() and onProgressUpdate())
use intent service to do that ask task means call ask task in a intent service that one is capable to handle background task without hang UI
in my app, I want to implement an image viewer. The viewer needs to download a large amount of images from my own server using urls.
I want to do this in background (not block the UI).
But the tricky part is I need to dynamically change the order of the downloading task by the main activity. For example, there are 3 tasks right now, and the user click a button in the main activity, then the app needs to add a new task, but also the new coming task need to be execute first (with highest priority)?
How should I do this?
thanks a lot!
Lucklily you csn't do any mess like you wanted on system level. Use IntentService with task queue and manage your background jobs there
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().
I'm writing a simple downloader app which has two activities:
SearchActivity: shows a list of items you can download given a query. pressing an item should start downloading the item, but stay in the SearchActivity. Pressing an item currently runs an AsyncTask to handle the downloading logic
DownloadsActivity: shows a list of currently downloading items and ProgressBars (with percent) for each item.
I've set everything up except the ProgessBars. I'm wondering what is the best way to have the ProgressBars in the DownloadsActivity update correctly. Within the download AsyncTask run within the SearchActivity, how do I update the respective progressbar in DownloadsActivity if DownloadsActivity is being viewed?
You should likely use the Service component for the downloads purpose. This would allow you to run AsyncTasks (or just separate threads) from within the Service itself. Afterwards, the status of the particular download may be retrieved by any Activity by invoking some method (written by you) on the Service object which, in fact, is managing all the downloads. Oh, and in the same way you may want to start downloads.
BTW, to retrieve Service instance object from within Activity, you need to bind to the service first and then get its instance object using the IBinder interface. Further info on Services could be found here.