Android :: How Does My Activity Knows When The Device Changes In Orientation
Sep 20, 2010How does my android activity know (is there a callback) when there is a change in screen orientation?
View 1 RepliesHow does my android activity know (is there a callback) when there is a change in screen orientation?
View 1 RepliesI have created an activity for my game which handles all orientation changes by itself and has a fixed "portrait" layout. Actually it uses the accelerometer and is rendered using 2D canvas methods. If the level has been completed I show up a highscore dialog in which the user can enter his name. The dialog is floating above the underlying level screen which gets blurred out nicely. This generally works.
Problem is that the dialog does not get rotated if the orientation of the phone changes. So even if the keyboard is exposed the dialog is shown in portrait mode instead of landscape. I have tried to use an activity with dialog theme instead but the behavior didn't change. After several tries it seems that I have found the reason for this: Once there's an activity with fixed orientation in the activity stack then all subsequent activities keep this orientation, too. They will not react on orientation changes anymore (e.g. if the keyboard gets exposed).
After upgrading to 1.5 SDK the described behavior changed a little bit. Now at subactivities indead react on orientation changes like expected. However, the need to have an opaque background! That means that neither dialogs nor activities with dialog theme will work. Those will stick with the orientation of the underlaying activity. So my workaround is to first start a sub-activity with an opaque black background. This activity then shows my highscore dialog on top. It looks quite okay but I wonder if there's a better solution? Is it possible to show a dialog above a fixed portrait or landscape activity which automatically adapts to orientation changes?
How do I map the device Y axis to the screen Y axis?I can't figure this out.I'm the developer of Tricorder, and I'm trying to make it so that the accelerometer and magnetometer indicators I draw always point in the right direction.On the G1 phone with the slider closed, all is well.But when I open the slider, I end up showing that gravity is pulling me to the left (if the phone is held with the display vertical).So, I know that the device, and hence sensor, Y axis is always in the direction of the earpiece; and the screen Y axis changes when I open the slider.And I know that I can use remapCoordinateSystem() to change the device axes to match the screen axes.But where do I find out what the device's orientation is?In other words, how do I compute the correct values for the X and Y parameters to remapCoordinateSystem()?
Configuration.orientation seems quite useless.Suppose it is set to LANDSCAPE.What does this mean?Does it mean a device which is "naturally" portrait (i.e. the sensor Y axis points to the narrow end), and which has been turned into landscape mode?If so, which way was it turned?Or does it mean a device which is naturally landscape?In other words, LANDSCAPE could mean that the screen axis is off from the sensor axis by +90, 0, OR -90 degrees.Not much help.So how do I do this?Obviously I want my app to work on all devices, not just the G1 phone.
When views with different type have same id and screen orientation changes,
either java.lang.ClassCastException: android.view.AbsSavedState$1 or java.lang.IllegalArgumentException: Wrong state class -- expecting View State will occur. (depends on the view's order)
Because View.dispatchRestoreInstanceState() checks id only.
You may wonder why anyone would make views with different type to have same id.
But it can happen when you use tab activity.
Imagine you have tab activity with two children activity.
Tab1 is ListActivity and Tab2 is ExpandableListActivity.
Both activity have id of "@android:id/list" but the type of view is different.
This means we cannot use ListActivity & ExpandableListActivit at the same in one tab activity.
I have a TabActivity, and each Tab corresponds to its own Activity. In one of them, in the onCreate method, I use startActivityForResult to show a dialog (specifically, Bump's BumpAPI activity).
code:...............
The problem is that when the screen orientation changes, it tries to create the tab's activity again which makes another BumpAPI dialog, resulting in multiple stacked on top of each other. Do I have a hook into the started activity to cancel the previous one when the orientation changes?
A workaround seems to be to add a button that when clicked, starts the second activity, but that adds an unnecessary step.
Also, I can't fix the screen orientation for the entire tabActivity because some of them require typing and users may want to use their physical keyboards.
I have a camera preview activity, which is forced to stay in landscape mode.Is there a way I can tell the current orientation of the phone (not the screen orientation).Basically I want to know if the user is holding the phone in portrait mode, or in landscape mode, so I can rotate the output captured bitmap correctly (right now it just always outputs in landscape mode).
View 1 Replies View RelatedI have set my deveiec property to landscape in manifest file android:screenOrientation="landscape" No at runtime when I am trying to get the current orientaion of device using Configuration and ActivityInfo classes. Its always giving me landscape mode even my device is in portrait mode. Please suggest if you have any method which gives device orientation info at runtime.
View 2 Replies View RelatedHow can i get the default orientation of a Android device.DROID has portrait as the default orientation whereas many of the android tablets have landscape as the default orientation. I want to determine the default orientation of a android device through an OS call and this orientation should be independent of the device position/ rotation.I will be of great help if somebody can suggest a mechanism to achieve the same.
View 11 Replies View RelatedI am writing an activity, that loads data from a server and displays it as a list using ArrayAdapter. For that I'm showing a progress dialog i.e loading, while it loads all data from the server. Then i dismiss the dialog in a handler. My problem is that when ever i change the orientation, the progress dialog is again shown, which is not needed, because all the data is displayed already?
View 1 Replies View RelatedHow do i find the current orientation of the phone (portrait or landscape) How do i find the slider position?
View 2 Replies View RelatedI know on screen orientation changes activity restarted, but suppose i don't want to restart the activity then what should I do? I had tried it by adding in manifest.xml. android:configChanges="keyboardHidden|orientation"
and override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); }
But still activity restarted each time when I change screen orientation.
I am having an interesting problem and would appreciate any advice. My app uses WebView as its primary view. Using a javascript hook, it can launch the camera so the user can take a picture. The camera is launched using: Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
When the camera starts, it makes sense to use landscape mode, so the user naturally turns the phone on its side. After the user takes a picture, control returns to my launching Activity (actually, my Acitivity gets restarted because it usually gets destroyed to free up memory for the camera on my Moto Droid). When my WebView gets recreated, I restore its state from the Bundle I saved in onSaveInstanceState().
Now everything looks ok, except the phone is still in landscape mode. However, when the user turns the phone upright bringing back into portrait mode, my WebView takes up only half of the screen. Somewhere along the way, the scale of my WebView got lost. This seems like such a minor issue, but it is driving me crazy.
Does anyone have an idea why this might be happening? What is the correct way to preserve the scale of my WebView? I am hesitant to hard code any scale factors because what looks good on my device may not be the same for another.
I finally got to the point where I was ready to deploy my app to my G1 and test it out and the first thing I noticed was that when I change my phone to landscape view my main activity restarts and resets all of my variables to their default settings. How can I avoid this?
View 7 Replies View RelatedMy main activity has some code that makes some database changes that should not be interrupted. I'm doing the heavy lifting in another thread, and using a progress dialog which I set as non-cancellable. However, I noticed that if I rotate my phone it restarts the activity which is REALLY bad for the process that was running, and I get a Force Close.
What I want to do is programatically disable screen orientation changes until my process completes, at which time orientation changes are enabled.
It is possible to set the orientation of an activity in the manifest file. but is it also possible to do it from code? if so, how?
View 2 Replies View RelatedI m currently working on landscape mode for android app in which i want to know how to maintain the state of the activity and views displayed on orientation change i.e from portrait mode to landscape mode ?
View 3 Replies View RelatedI am working on a MVC implementation for Android (to be subsequently hosted on SF and/or GC).
It works like this:
Activity (View) <=> Application (Controller) <=> Data (Model) <=> Persistence (DB/Network etc)
The scenario is:
1. Activity launch (main/launcher)
2. Notifies Application about performing a data transaction
3. Upon receipt of data response, controller devices which Activity to launch (or update existing)
4. Application has overridden the method onConfigurationChange
Problem: When the orientation is changed, the Application is notified about onConfigurationChange but:
a) The "current" activity is Destroyed and recreated -- which is fine to some extent b) The new instance which is created is automatically => onCreate, onPause, onStop, onDestroy.... now that's catastrophic.
My MainActivity (the one that is started by the android application) is a TabActivity which contains several tabs (which are implemented as activities as well). In a certain tab/activity I want to prevent the change of orientation/rotation when the phone is turned. What is the easiest way to achieve this?
View 2 Replies View Relatedi've developed an android app that's fetches an xml file and displays this data via several markers on the map. This works fine so far. The problem right now is that when i switch the orientation of the phone (portrait->landscape or vice versa) the markers disappear for a small moment, the xml processing is started again and then they reappear. Is there a way to prevent this re-loading of the file? It only takes about 2-3 seconds.
View 2 Replies View RelatedCan I temporarily disable automatic orientation changes in my Activity? I want to do this when my app does some background internet communication.
View 1 Replies View RelatedI have Activity with ListView inside it and in the onCreate method of the Activity I have code for populating the Data of the ListView this Data is a server based and so populating includes calling Network URLs. I have the ArrayAdapter of the ListView in the Same Activity Class.
Now the Issue I'am facing is that, in Rest all scenarios my Activity is behaving in a proper way but when the Orientation [ Portrait to Landscaped or other way round] is taking place the Data is Getting lost and Newer Data calls are Required to Populate the Same Old Data now this is something that is not intended out the code how should I deal with it.
I have an Android Activity with a RelativeLayout and I have implemented the following method to prevent the activity from being recreated on change of Orientation:
CODE:..............
I am obviously not doing anything in this method, but it worked perfect when using a LinearLayout. Now however, using RelativeLayout, my layout is all messed up when changing to landscape orientation.
What is the most efficient way to have the screen redraw correctly without having the activity restarted again with a call to onCreate?
I'm trying to find a way to properly handle setting up an activity where its orientation is determined from data in the intent that launched it. This is for a game where the user can choose levels, some of which are int portrait orientation and some are landscape orientation. The problem I'm facing is that setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) doesn't take effect until the activity is fully loaded. This is a problem for me because I do some loading and image processing during startup, which I'd like to only have to do once.
Currently, if the user chose a landscape level:
the activity starts onCreate(), defaulting to portrait discovers from analysing its launching Intent that it should be in landscape orientation continues regardless all the way to onResume(), loading information and performing other setup tasks at this point setRequestedOrientation kicks in so the application runs through onPause() to onDestroy() it then again starts up from onCreate() and runs to onResume() repeating the setup from earlier
Is there a way to avoid that and have it not perform the loading twice? For example, ideally, the activity would know before even onCreate was called whether it should be landscape or portrait depending on some property of the launching intent, but unless I've missed something that isn't possible. I've managed to hack together a way to avoid repeating the loading by checking a boolean before the time-consuming loading steps, but that doesn't seem like the right way of doing it. I imagine I could override onSaveInstanceState, but that would require a lot of additional coding. Is there a simple way to do this?
Solution:
As per Daniel's answer, this was actually quite easy to fix. I just needed to make a few small changes. In my 'menu' Activity, where the player would choose which level to play, I just had to add an if/else check to choose which class would be started by my Intent. This was done with a simple int representing portrait or landscape, determined when the player selected a level. I then created a second class extending my 'GameLogic' class; this is the class which contained most of the code for the game itself, rather than the menus, instructions, etc.
public class GameLandscape extends GameLogic{}
Literally that simple and completely empty. That way it inherited all the code from my previous activity where I had already coded it to handle things differently depending on the orientation. Lastly I just had to add a line to the manifest stating that GameLandscape would always run in landscape, and GameLogic would always run in portrait.
I have a requirement where I have to read/parse some data from the server(using HTTPConnection) and then display it on to the screen. To avoid ANR issue I used AsyncTask to perform the above. Now in order to avoid application crash in case the activity gets destroyed(either by OS or if orientation changes or say on any incoming call-->where user stays on the phone for long and for some reason OS destroys/recreates the activity). Currently, I hols the reference of AsyncTask in my activity and then provide a callback(the activity itself) to AsyncTask so that I can pos back message to activity(like closing progress dialog box).In order to avoid memory leaks on onPause I nullify this activity reference in AsyncTask.Before onPause I save the AsyncTask instance using onSaveInstanceState method but I have to use putSerializable for that. I have read on the forums that serialization method is slow, so my question is should I use parcelable approach(and whether the converting AsyncTask to parcelable will be same as creating any other object parcelable). or both these approaches are totally wrong i.e...............
View 10 Replies View RelatedIt seems when screen orientation changes, the activity's onCreate() method is called. In my onCreate() method, I have an AlertDialog which pops up when the activity is called. The problem is when I switch the screen sideways, the popup is displayed again. How can I avoid this?
View 1 Replies View RelatedIn onCreate method of Activity, it binds to a remote service and makes use of AIDL intefaces. Is it required to unbind from the remote service when onDestroy is called on screen orientation change.?
If the activity un-bounded from the remote service in onDestroy and if no other contexts are bound to remote service, is remote service likely to get stopped losing the state that it maintains.?
I'm testing an app on a Droid. It's a kind of card matching game. I'd like to keep the cards physically located in the same place on the screen regardless of a screen rotation. For example, I can put my thumb on a card, rotate the phone any way I please and the card stays fixed under my thumb. However, the orientation of the content of the card will change to match the current phone rotation. I handle this by transforming the card grid in onCreate. This works fine for 90 degree rotations, but if I do a fast 180 degree flip from one landscape mode to another the screen simply flips and the Activity is NOT destroyed and recreated.
This seems contrary to the documentation:
http://developer.android.com/reference/android/R.attr.html#configChanges
"public static final int configChanges Since: API Level 1
Specify one or more configuration changes that the activity will handle itself. If not specified, the activity will be restarted if any of these configuration changes happen in the system. Otherwise, the activity will remain running and its Activity.onConfigurationChanged method called with the new configuration. "
I am NOT specifying any configChanges in my AndroidManifest.xml file, so it seems that the activity SHOULD be destroyed and recreated. Why isn't it? Is there some way I specify that it should be destroy/ recreated?
I have a notification that starts my activity and passes a messages using the intent's putExtra() function. The message is then displayed to the user in the activity's onCreate function. When the application is restarted due to a orientation change, the message is shown again as it is still in the intent's bundled data.
How can I remove the extra data?
I tried the following:
Bundle bundle = getIntent().getExtras();
if (bundle.getBoolean("showMessage")) {
// ... show message that is in bundle.getString("message")
// remove message
bundle.remove("showMessage");
}
But the message will still be shown after the orientation changed, seems like the intent used is not the one I changed, but the original one. The only workaround I found is to save the showMessage additionally in onSaveInstanceState(). Is there another way? Or is this the way to go?
I have an app that works only in portrait mode, and I have made the changes in my manifest file for every activity the orientation to be portrait. But when I rotate the device, the activity recreates again.
How to not destroy the activity?
I understand how to force a specific orientation per activity in the android manifest. I would like to know if it is possible to allow the keyboard to change orientation even though the activity(the layout really) must remain unchanged.
View 1 Replies View Related