I have a gestureOverlayView in my layout which I am using for drawing digital Signature. The problem arises when I draw in a certain orientation (say landscape) and then change the orientation - the overlayView just clears. I have tried including onConfigurationChanged();but no effect. I have also attempted the following with onSaveInstance and onRestoreInstance, but it gives me no solution:
#Override
protected void onSaveInstanceState(Bundle outState) {
Gesture gesture = overlay.getGesture();
outState.putParcelable("gesture", (Parcelable) gesture);
super.onSaveInstanceState(outState);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
Gesture gesture = (Gesture) savedInstanceState.getParcelable("gesture");
overlay.setGesture(gesture);
mDoneButton.setEnabled(true);
super.onRestoreInstanceState(savedInstanceState);
}
I have also tried including:
android:configChanges="fontScale|uiMode|screenLayout|navigation|touchscreen|mcc|mnc|orientation|keyboardHidden|keyboard">
But even this is of no use.
Does anyone have a solution for this problem?
I tried your code in my application and had the same issue. The result of getGesture() is null within onSaveInstanceState, onStop, and onPause, so I'm guessing that the GestureOverlayView is invalidated somewhere between the screen rotation and the calling of those methods.
A workaround is to override onGesturePerformed, assuming your Activity implements OnGesturePerformedListener and you call addOnGesturePerformedListener(this) on your overlay, and save a member instance of the most recent Gesture. Something like:
#Override
public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
mCurrentGesture = gesture;
}
Then in your onSaveInstanceState, simply get and package the member instance instead of calling getGesture(). Now setGesture() should work as expected when the Activity recreates itself.
Use android:configChanges="fontScale|uiMode|screenLayout|navigation|touchscreen|mcc in manifest or try onSavedInstance;
When the orientation changes it restarts the activity. You could try restrict the orientation of the app in the manifest file.
android:screenOrientation="portrait"
Related
I've got my app working for the most part, but I've got buttons and text views with text that change based on some state variables. When I change the device's orientation it destroys and recreates the activity in the new orientation. I've tried adding
android:configChanges="orientation"
to the manifest file. I've also tried overriding the onConfigurationChanges method to "do nothing" but the text still reverts to default.
I know I can lock the user in to one orientation, but I would rather have the app usable in either orientation.
Alternatively, is there a way to determine which orientation the user opened the app in and lock them in that orientation until they restart the app?
Edit:
Thank you Kabir,
android:configChanges="orientation|screenSize"
works perfectly
For API 12 and below:
android:configChanges="orientation"
if you are targeting API 13 or above
android:configChanges="orientation|screenSize"
Actually orientation changing works by destroying and recreating an activity. Some views are able to save theirs states, others no. TextView doesn't save its state (in this case text) as it tends to show static text. If you want to save TextView's state during the configuration changes, you can do as following:
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
/*
1st argument is key, 2nd is value to save
*/
outState.putString("savedText", myTextView.getText().toString());
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
/*
The argument is key to get saved string
*/
myTextView.setText(savedInstanceState.getString("savedText"));
}
These onSaveInstanceState() and onRestoreInstanceState() are Activiy's methods.
I am using the Google Maps API in my Android app, and upon orientation change, the map redraws itself every time since the Activity is destroyed. How does the official Google Maps app prevent this from happening in their app? The map just simply shifts orientation without any jarring disappearing of the map. Are they using a complicated custom onConfigurationChanged() method, or is it something simpler? Thanks.
They handle config changes themselves. The activity is setup as follows
<activity
android:configChanges="orientation|uiMode|screenSize|fontScale"
android:screenOrientation="user"
...
and then implements onConfigurationChanged. In there they most likely do extensive layout animations so it looks smooth.
Try using an onSaveInstanceState method in your activity and restore your values in onRestoreInstanceState. This is the example in Android developers page:
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Save the user's current game state
savedInstanceState.putInt(STATE_SCORE, mCurrentScore);
savedInstanceState.putInt(STATE_LEVEL, mCurrentLevel);
super.onSaveInstanceState(savedInstanceState);
}
And then:
public void onRestoreInstanceState(Bundle savedInstanceState) {
// Always call the superclass so it can restore the view hierarchy
super.onRestoreInstanceState(savedInstanceState);
// Restore state members from saved instance
mCurrentScore = savedInstanceState.getInt(STATE_SCORE);
mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);
}
Check the documentation here -> http://developer.android.com/intl/es/training/basics/activity-lifecycle/recreating.html
Add to your AndroidManifest.xml inside the relevant <activity> tag:
<activity
...
android:configChanges="keyboardHidden|orientation|screenSize"
...>
Note that this means that onCreate will no longer be called on orientation change. If you still need a callback, use onConfigurationChanged like so:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// your code here
}
Additional hint: In onConfigurationChanged your map will still expose its old projection and bounding box (from before the orientation change), in case you need to do something with the projection check my other answer.
Im really new to android and i have a little problem that i dont know how to solve.
Im having a small application that prints out the Activity lifes circles methods like this:
protected void onCreate(){
super.onStart()
print("onStart was called"); //this is a void and its only printing a text
}
protected void onStart(){
super.onStart()
print("onStart was called");
}
and so on...
While im i portrait mode the app is showing all the methods on the screen but when i switch to landscape the activity object is of course destroyed and it creates the first three methods again.
Im using onSaveInstanceState an onRestoeeInstaceState to try to save printed order on the screen while i switch from portrait to landscape.
How can i make it work?
example of app output in portrait mode:
onCreate was called
onStart was called
onResume was called
onPause was called
onStop was called
onRestart was called
onStart was called
onResume was called
i want theese prints to stay even if i switch to landscape.
This is onSaveInstanceState and onRestoreInstanceState i dont really know how to solve the problem here.
#Override
protected void onSaveInstanceState(Bundle savedInstanceState){
super.onSaveInstanceState(savedInstanceState);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState){
super.onRestoreInstanceState(savedInstanceState);
}
If you go to the manifest I believe you are able to edit it to allow portrait and landscape displays and also have it recall the savedInstanceState. Sorry I can't give you a more detailed answer at the moment.
I need to handle orientation changes in my Android application. For this purpose I decided to use OrientationEventListener convenience class. But his callback method is given somewhat strange behavior.
My application starts in the portrait mode and then eventually switches to the lanscape one. I have some custom code executing in the callback onOrientationChanged method that provides some additional UI handling logic - it has a few calls to findViewById.
What is strange is that when switching back from landscape to portrait mode onOrientationChanged callback is called twice, and what's even worse - the second call is dealing with bad Context - findViewById method starts returning null. These calls are made right from the MainThread
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
listener = new OrientationListener();
}
#Override
protected void onResume() {
super.onResume();
// enabling listening
listener.enable();
}
#Override
protected void onPause() {
super.onPause();
// disabling listening
listener.disable();
}
I've replicated the same behavior with a dummy Activity without any logic except for one that deals with orientation hadling.
I initiate orientation switch from the Android 2.2 emulator by pressing Ctrl+F11
What could be wrong?
Upd:
Inner class that implements OrientationEventListener
private class OrientationListener extends OrientationEventListener {
public OrientationL() {
super(getBaseContext());
}
#Override
public void onOrientationChanged(int orientation) {
toString();
}
}
}
This is a documented bug in the emulator ONLY. A real device will not exhibit this double-lifecycle-events behavior. I had the same issue a while ago and it disappears on a real device.
I would suggest ignoring the problem if you can by only testing orientation changes in one direction until you get your hands on a physical phone. Otherwise you might be able to "skip" the second set of lifecycle calls by keeping a static boolean around indicating you've already gone through the first set.
See this issue report for more info.
Have you tried using onConfigurationChanged?
#Override
public void onConfigurationChanged(Configuration newConfig) {
if(newConfig.equals(Configuration.ORIENTATION_LANDSCAPE)…
Add android:configChanges="orientation" in manifest file in activity tag like
<activity android:label="#string/app_name"
android:configChanges="orientation"
android:name=".com.androidpeople">
I need to handle orientation changes in my Android application. For this purpose I decided to use OrientationEventListener convenience class. But his callback method is given somewhat strange behavior.
My application starts in the portrait mode and then eventually switches to the lanscape one. I have some custom code executing in the callback onOrientationChanged method that provides some additional UI handling logic - it has a few calls to findViewById.
What is strange is that when switching back from landscape to portrait mode onOrientationChanged callback is called twice, and what's even worse - the second call is dealing with bad Context - findViewById method starts returning null. These calls are made right from the MainThread
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
listener = new OrientationListener();
}
#Override
protected void onResume() {
super.onResume();
// enabling listening
listener.enable();
}
#Override
protected void onPause() {
super.onPause();
// disabling listening
listener.disable();
}
I've replicated the same behavior with a dummy Activity without any logic except for one that deals with orientation hadling.
I initiate orientation switch from the Android 2.2 emulator by pressing Ctrl+F11
What could be wrong?
Upd:
Inner class that implements OrientationEventListener
private class OrientationListener extends OrientationEventListener {
public OrientationL() {
super(getBaseContext());
}
#Override
public void onOrientationChanged(int orientation) {
toString();
}
}
}
This is a documented bug in the emulator ONLY. A real device will not exhibit this double-lifecycle-events behavior. I had the same issue a while ago and it disappears on a real device.
I would suggest ignoring the problem if you can by only testing orientation changes in one direction until you get your hands on a physical phone. Otherwise you might be able to "skip" the second set of lifecycle calls by keeping a static boolean around indicating you've already gone through the first set.
See this issue report for more info.
Have you tried using onConfigurationChanged?
#Override
public void onConfigurationChanged(Configuration newConfig) {
if(newConfig.equals(Configuration.ORIENTATION_LANDSCAPE)…
Add android:configChanges="orientation" in manifest file in activity tag like
<activity android:label="#string/app_name"
android:configChanges="orientation"
android:name=".com.androidpeople">