0

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

Musterknabe
  • 5,763
  • 14
  • 61
  • 117

3 Answers3

1

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...

Mikel
  • 1,581
  • 17
  • 35
  • Your guess is correct. I can't show the code, I'm sorry. But I don't think it would be any helpful, since it's just when a view is assigned. Nothing more. So I would try to cancel the asynctask? How do I do that? Simply `asynctask.cancel(true)`? – Musterknabe Oct 24 '13 at 08:48
  • Yes, but check if it is canceled also. Check this http://stackoverflow.com/a/6053943/562840 – Mikel Oct 24 '13 at 08:49
  • That's where the funpart begins. I start the fragment, the onCreate checks if the async task is canceled before. It's not of course and therefore it executes the async task. Now, when I click on the nav menu item the fragment is started again from scratch and then the asynctask isn't cancelled. So I somehow have to cancel it from the activity where the async task isn't located. How do I do that/ – Musterknabe Oct 24 '13 at 08:55
  • When you press the nav menu item a new instance of the fragment is started, right? The previous instance should still pass in the lifecycle methods, so you can cancel the asynctask from the fragment that started it when it is paused/destroyed/detached something like that. You can also check if the task is canceled in the postexecute method and break the cycle if it is. – Mikel Oct 24 '13 at 09:02
  • Thanks. That helped. I cancelled it on the paused method! That worked :) Thanks very much! @Mikel - Can you please edit your answer so people see what helped me actually and don't have to read through all comments? :) – Musterknabe Oct 24 '13 at 09:03
0

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())

Eddy
  • 489
  • 4
  • 10
  • PostExecute is just running a for-loop that adds views to a layout. – Musterknabe Oct 24 '13 at 08:59
  • And layout is part of the Fragment that calls the AsyncTask? When you change fragment it will destroy the old fragments View, so you lose the Layout of that fragment. Also about Canceling a task. when you cancel a AsyncTask it anyway calls onPostExecute() so you can get the same crash.And if the layout where you add views in for-loop is part of the destroyed fragment, you better return from the onPostExecute ,via an Interface ,data you got from your source and make all the changes in the class that implements Interface.Also you will need a manager that will change the instance – Eddy Oct 24 '13 at 09:07
  • of the class that will handle AsyncTask responses – Eddy Oct 24 '13 at 09:09
0

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

Bhanu Sharma
  • 5,135
  • 2
  • 24
  • 49