7

I have an Activity that runs a simple AsyncTask. To keep the AsyncTask running during screen rotations I am using onRetaingNonConfigurationInstance() to pass the AsyncTask to the new Activity as recommended in one of the answers from the thread How to handle an AsyncTask during Screen Rotation? Then I update a pointer to the Activity in the AsyncTask to make sure it can still update the progress dialog from on onProgressUpdate().

My problem is that using this method I cannot detect if the Activity is only rotated (In this case I want to continue to run the AsyncTask) or if the Activity is hidden by the Home button in which case I'd like to stop the AsyncTask. I can however detect the usage of the back button because in this case the isFinishing() of the Activity returns true;

Is there any mechanism I could use to differentiate rotation versus home button usage?

Community
  • 1
  • 1
jmbouffard
  • 1,581
  • 1
  • 15
  • 22

3 Answers3

2

If you press the home button the method onRetaingNonConfigurationInstance() shouldn't be called. So you can use some boolean variable which you set in onRetaingNonConfigurationInstance() to true. In onDestroy() you can then check the variable and stop the AsyncTask when it is still false.

Update

I thought about my solution again and after some searching I think it is wrong, as onDestroy() won't be called if you press the home button. And onStop() is called before onRetaingNonConfigurationInstance() so stopping the AsyncTask there will be to early as you don't know if it is a configuration change or not.

An ugly work around would be to call a method on you AsyncTask in oStop() which initialize some sort of self destruction on the AsynTask which you can then stop again in onRetaingNonConfigurationInstance() if it is a configuration change. As I said it is not very elegant.

Flo
  • 27,355
  • 15
  • 87
  • 125
  • 1
    Thanks for the proposal but I will wait a little bit to see if someone provides a more "elegant" solution before to choose this as my accepted answer. – jmbouffard Apr 27 '11 at 14:17
  • @jmbouffard - You may be over analyzing this. Your are already using onRetainingNonConfiguraitonInstance() to handle rotations. Make sure to cancel the AsyncTask in onDestory(). That should be all that is needed. IF user presses home and phone does not kill your activity (and call onDestroy()) then reloading activity will have it loaded and ready to go for the user. – eyespyus Jun 02 '11 at 04:47
  • 1
    It would not work for me because I want my AsyncTask to continue whenever the screen rotation occurs and onDestroy() is called at every rotations. I want to pass my active AsyncTask to the new Activity that is created after the rotation. – jmbouffard Jun 03 '11 at 13:28
0

Here I found a very elegant solution how to use AsyncTasks with ProgressDialogs. I cannot say now if it serves your case. Try it.

Yury
  • 20,618
  • 7
  • 58
  • 86
0

You have to add this to the activity declaration in the manifest:

android:configChanges="keyboardHidden|orientation|screenSize"

That should keep all your activity unchanged when rotating.

Hope it help

Berserkore
  • 21
  • 2