tags:

views:

90

answers:

3

Hi guys,

I am getting error when I rotate the device (it was HTC with Verizon):

The application My App (process com.mycompany.android) has
stopped unexpectadly. Please try again.

So, I think I need to code for this issue. In order words, when rotate the device from landscape to portable or from portable to landscape, application needs to catch this event.

Is it right ?

How to make it ? What is wrong here ?

Edit: Is it possible to test on the emulator ?

Thanks in advance.

+1  A: 

If I'm not mistaken, Android (by default) destroys and re-creates your Activity when you change orientations. Does that sound like it might cause a problem in your application? There's an article on the Android Developer's blog, and there is a post on the Android Developer's mailing list that describes a similar problem.

Daniel Yankowsky
Thanks for response. It sounds like difficult to solve for my issue. Because when current activity starts, this activity connects to ftp server. Then it generates data to use in a thread. So, every in rotation do I need to connect to, to process .... ?What way is the best ?Please advice guys.
AndroiDBeginner
You can use an `AsyncTask` to do stuff in the background from your `Activity`, or create a `Service` which holds open your connection and bind to it from your `Activity`.
Christopher
Ok. Thanks. I will try your idea.
AndroiDBeginner
+1  A: 

By default Android activities are shut down and restarted on a configuration change. The easiest way to get around this is to add android:configChanges the the application manifest. This will tell Android that you will handle the listed config changes yourself. For example

<activity
    android:name=".YourActivity"
    android:label="YourActivity"
    android:configChanges="orientation|keyboardHidden" />

In your manifest means the activity will not restart on an orientation change or if the user has slid out a keyboard.

wf
Great, I was searching this. I got it. Thanks.
AndroiDBeginner
+1  A: 

In the case when it is known that a new instance will immediately be created for the new configuration.

The Activity function onRetainNonConfigurationInstance() allows you to store extensive state from the old to new activity instance, from loaded bitmaps, to network connections, to evenly actively running threads.

After implementing this method the result can be used in the onCreate() or onStart() with the function getLastNonConfigurationInstance().

Rene
Thanks. It is very helpful thing for me.
AndroiDBeginner
Good to hear. Btw, the stackoverflow system works best when pressing the uparrow above the number left of useful answers. :)
Rene