0

I am making an android app, which needs to download a text from a url and show it on a alertdialog.

I created a new thread. The thread reads a file on the web, a progressdialog appears before the thread starts, the thread packages the file content from the web on bundle , and passes it to a handler, the handler stops the progress, and shows the text from the bundle on a alertdialog. Well this works pretty good, but when the screen orientation changes when the progressdialog is shown and the thread is running, the app crashes. Any ideas on how to fix this thing?

Any help will be appreciated. :)

KSubedi
  • 443
  • 1
  • 7
  • 17

4 Answers4

2

Add activity to menifest file as following -

<activity 
    android:name=".MyActivity" //Replace  MyActivity with Your activity
    android:configChanges="orientation|screenSize|screenLayout">
</activity>
MD SHAHIDUL ISLAM
  • 14,325
  • 6
  • 82
  • 89
2

When the orientation changes, normally the OS will shut down your activity completely and restart it. That will mess up your download thread. One solution that might work is to save the Thread object as non-configuration data. You'll need to override onRetainNonConfigurationInstance() in your activity. However: do not do this if your Thread object has any references to any view- or activity-related object. That will cause a huge memory leak and will make your users very unhappy very quickly.

See the guide topic Handling Runtime Changes for details on using onRetainNonConfigurationInstance() and also an alternative approach. See the article Avoiding Memory Leaks on ... well, just that.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • thanks, ill try and reply what happens. but i wonder why android sdk makers made it so gay, couldnt they just make the screen rotate instead of recreating the whole activity. just that small thing makes the whole stuff complicated. :) – KSubedi Mar 09 '11 at 03:42
  • I think their thought was that by default you want this behavior. When the phone configuration changes (including orientation), your activities could potentially have an entirely different set of resources bound to them. The easiest thing is just to restart them all. When that's not the easiest, it's easy enough to specify in the manifest that the activity will handle orientation changes itself. However, actually handling them yourself is not easy at all, particularly if you have orientation-specific resources, which is why the default behavior is what it is. – Ted Hopp Mar 09 '11 at 04:01
0

This usually happens because your Activity actually gets destroyed and recreated on orientation changes.

Your options are:

  1. Embrace the destruction, and use an AsyncTask instead of a Thread to fetch your data. You cancel() the task in your onDestroy() method.

  2. Also use AsyncTask and cancel() it in your onDestroy() method. Additionally, handle configuration changes such as in the answers to this question.

Community
  • 1
  • 1
Matthew
  • 44,826
  • 10
  • 98
  • 87
  • What if I sent an http request to a server, server received it and made necessary changes, at this time screen rotated, after that server sends response that changes were made. Nobody will receive response. What is the workaround here? Thanks. – Maxim Sep 01 '11 at 21:39
0

KSubedi... Assumptions can be dangerous. Step by step testing to eliminate the exception may reveal that it is the running progress dialog that is causing the exception. The cure is:

protected void onPause() {
        super.onPause();
        //if (asynch != null) {asynch.cancel(true);}
        if (progress != null){progress.cancel();}
    }

JAL

JAL
  • 3,319
  • 2
  • 20
  • 17