Android application stop unexpectedly when rotate - android

I am new to android application.
My Android application works well on emulator but running on real android device (Softbank 003 SH), when I rotate the device from portrait to landscape, the application stop unexpectedly.
Do you have any hint to solve this problem?

when rotate the device from landscape to portrait will re-create the activity so thread will stop and if any builder is running will cause a error so :
Start by adding the
android:configChanges node to your Activity's manifest node
android:configChanges="keyboardHidden|orientation"
Then within the Activity override the onConfigurationChanged method and call setContentView to force the GUI layout to be re-done in the new orientation.
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(R.layout.myLayout);
}

You can simulate rotation on the emulator with CTRL+F12.
As for the "unexpected" stop, it is because when you rotate, the Activity is reloaded (onCreate is run again, etc), and you probably didn't follow the Android way of coding and some of your variables end up uninitialized! It is a very common error to assume Android works like Windows (has applications), but very generally, it works more like the iPhone or dynamic webpages (has semi-independent forms).
Check the LogCat for the error.

Related

android orientation - issues with intents (camera)

Here is an interesting one...
I have an application I am writing for devices running android 2.3.3 and above.
It has a main activity which calls the camera via an Intent.
If the user clicks a button to launch the camera; then takes a picture; then clicks "Done" to return to the main activity - the application works fine and displays the new picture in an imageview on the main activity.
however, if the user uses the main activity in portrait orientation; then clicks the button to open the camera intent and changes the orientation to landscape; then click done to return to the main activity - the application crashes
but then...however, if the user uses the main activity in portrait orientation; then clicks the button to open the camera intent and changes the orientation to landscape; then changes the orientation back to portrait (in the camera) before clicking done to return to the main activity - then the application continues to run normally.
Stuff I have tried:
I have changed the manifest file to force the application (main activity) to be oriented in portrait (I have also removed this)
I have added this line to the main activity in an attempt to handle the re-drawing of the activity on it return from the camera intent:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
In the debug window I get errors describing nullpointer exceptions - I think this is because the views are no longer there after an orientation change for the image to be passed into unless the orientation is the same as when it was left.
I'm a bit stuck so would appreciate some advice.
When your screen orientation changes, your Activity is destroyed and recreated in the new orientation. So any variables that gained a reference during the life of the Activity will no longer refer to anything, and if you then try to access objects they refer to without re-assigning them values, you'll get a NullPointerException.
The method onSaveInstanceState() is used to save temporary data between configuration changes. This creates a Bundle which is passed to onCreate() when the Activity starts up again.
Without any code, I don't know if that's your problem, but it's worth a look.
See http://developer.android.com/reference/android/app/Activity.html#ConfigurationChanges for more information (and more accurate information than I've provided, no doubt).
hey i am not sure but try to put this property in manifest file hopefully it should work
android:configChanges="orientation|keyboardHidden"
Some Android devices like Samsung S3 and S4 have default camera surface view in Landscape mode.
When you call camera and capture image and come to your application, whether you had set orientation PORTRAIT it will first open in LANDSCAPE mode and then changes to PORTRAIT mode.
Its Android OS behaviour.
Due to this Activity Re-Creates it self. At that time you are getting Null Pointer Exception.
You can handle by setting configChanges in menifest file.
android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
and you can store the data temporary in onSaveInstanceState() rightly said by #Spinner in his answer.

Android disable Activity to request while changing mode (prtrait/landscape)

I have Acivity which in it's onCreate method makes requset in Asynctask to remote server, takes data and renders them. It's ok, but when I'm trying to change mode to landscape - hall this process starts from the very begining and I have to wait untill data come from server one more time. I don't need it. I need just renderng the same data to landscape mode. How to do it?
hall this process starts from the very begining
It is happening because your activity is restarting after orientation.
So to prevent restart do following.
Make following changes in your activity in manifest.
<activity android:name=".ActivityName"
android:configChanges="orientation|keyboardHidden|screensize" />
ScreenSize attribute seems to be added in 4.0 so don't mentioned it if you are running below 4.0.
And then add the following method to Your Activity:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
It will prevent your activity from restarting.

Android: set activity orientation BEFORE onCreate, but not in manifest (HDMI plugged in issue)

I can perfectly manage orientation change programatically, by using setRequestedOrientation in onCreate.
Everything works really fine, until I plug a HDMI cable. After this, the tablet "wants" to be in landscape mode. So when I open an activity, it shows first in "landscape" and then right after it shows in "portrait", (because I'm calling setRequestedOrientation(variable_with_orientation_desired_by_the_user) where variable=ActivityInfo.SCREEN_ORIENTATION_PORTRAIT in onCreate As you can "see", my app keeps rotating on every activity the user opens;
If I set the orientantion for activity in the manifest, this problem is partially gone, since the orientation in manifest is the same that the user has picked up. If they are different, the same behavior starts again.
I tried this, but with no success:
#Override
public void onCreate(final Bundle saved)
{
setRequestedOrientation(ScreenOrientation);
super.onCreate(icicle);
}
So, is there any way to tell to android what orientantion it must create my activity, before it does create it? (But it cannot be in manifest)
I solved it by setting screenOrientation to "behind" in manifest file:
android:screenOrientation="behind"
I solved it using this declaration in manifest:
android:screenOrientation="locked"
on every activity declared in manifest with this issue.
And keep using setRequestedOrientation() programatically to define if landscape or portrait orientation within onCreate() method,
Hope it helps!

Android screen orientation change have different behaviour on tablet

I have activity with:
android:screenOrientation="portrait"
and code:
if(blah blah blah)
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)
in activity onCreate.
1. When test on smartphone "setRequestedOrientation" causes destroy activity and create again with desired screen orientation.
2. When test on tablet device this code runs only once activity. After "setRequestedOrientation" it just continues without destroy and create again activity. This happens when start code without debuger or without breakpoint in onCreate. If I stop in onCreate function - I see in logs - destroying and onCreate again.
How to catch specific situation with tablet?
UPDATE:
- Problem with tablet occurs when 'Orientation lock' if off. If 'Orientation lock' is enabled program works in same way as in smartphone.
UPDATE2:
- Is it possible to set preffered screen orientation to whole application. So when activity starts to know desired orientation. This will make unnessesary call 'setScreenOrientation' and avoid second activity creation.
After whole day spent with this issue I made following changes in my application:
1. Every activity /without first/ are declared as android:screenOrientation="behind".
2. In first activity I set preferred screen orientation using user selected mode /from my menu.
3. I call setRequestedOrientation only when user changed mode in my menu options.
This causes activities to be created only once. Finaly :)
No.. you cannot set preffered screen orientation for whole application... instead you should call for setScreenOrientation in respective activies only... Refere this link here...
You can set
<activity ActivityName = "test_activity"
android:configChanges="orientation">
</activity>
in your android manifest file. It will not allow activity to restart on orientation change but onconfigurationchange will be called normaly.
Later you can getdisplayorientation in onconfigurationchange and set whatever you want.

Avoiding Application Restart when Hardware Keyboard Opens

I am currently working on a multi-threaded game application for the Android platform... so far so good... I just got over a bug in my application which caused it to restart on orientation change (fixed by designating a specific orientation depending on the availability of a hardware keyboard or not, which is important 'cuz it is an online game with chat capabilities), and that works... BUT now i am trying to avoid the same problem when the user simply slides open the hardware keyboard. I'm not quite sure how to go about avoiding the restart of my application or the saving of the state of my application. Any solutions/suggestions?
In your <activity> tag in your manifest:
android:configChanges="orientation|keyboardHidden"
In your activity class:
#Override
public void onConfigurationChanged(final Configuration newConfig)
{
// Ignore orientation change to keep activity from restarting
super.onConfigurationChanged(newConfig);
}

Categories

Resources