Make Android Screen Rotation configurable application wide - java

I have a switch in my Android app settings to turn screen rotation on and off - just the way the user likes it to be.
But it does not disable in my app. I tried
_activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
but rotation is still on. I know I can do this in the manifest file, but I want it to be configurable and not static.

You need to set setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); inside onCreate() method in your activity.
Or since that method is not always called you could put it in the onResume() method of your activity.

Related

Refresh of screens on backgrounding and foregrounding an application

I am working on an Android app with multiple activities. When moving from one activity to another in certain cases I want to refresh the display but not in others.
One case is where I background the application and foreground it again. When I foreground it, I want to refresh everything on the screen depending on which activity
I backgrounded to begin with. How can I do this? I am unfortunately a bit new to Android so some appropriate basics where applicable would also be helpful.
http://developer.android.com/guide/components/activities.html#SavingActivityState
You can use onSaveInstanceState() to save information before onPause() is called. In onResume() you can use the saved info as a case in a switch statement or some conditional to refresh what you want.

Calling onCreate() from locked screen

I am developing an android emergency app and I want to add a feature that can call a certain method from my activity class when the screen is locked. Is there anyway for my app to detect any kind gestures like shake, screen tap pattern or any kind of movement when the screen is locked?
Not sure if it will work, but I think you can put a listener in a service and put the servicee in the foreground. I have no clue what exactly youwant to achieve but this is what I can think of right now. BTW -- pass information from a service the foreground activity

Android screen on during entire application

I'm writing an Android app that connects to a computer and acts as a mouse. I want the screen to remain awake only while the app is being used. I know I can use getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
to keep the screen on during an activity, but is there a way to keep the screen on for all activities or would I have to use that line of code in every onCreate method for each activity?
You would have to use that line within each activity.
Or you could use Fragments and set it once in the only activity and it will be applied to each fragment from then on.

Detect configuration changes in onStop() method without using isChangingConfigurations()

Currently I'm writing Android app using API level 10. I need to detect configuration changes (Screen rotation) in onStop() method. I cannot use isChangingConfigurations() method because it is only available in API level 11 or above.
How can I detect configuration changes manually in onStop() method?
can you please explain more deeply your question because I cannot see the case here...I mean onStop() as you can read here http://developer.android.com/reference/android/app/Activity.html#onStop() is called when your activity is no longer visible for the user. It could be never called also. So I cannot see how your are going to detect any UI behavior when your activity is gone.
If you want to detect some UI behavior you need to declare that in the android:configChanges attribute in your activity declaration in manifest. You can see docs here http://developer.android.com/guide/topics/manifest/activity-element.html
In your case (Screen Rotation) you will need android:configChanges="orientation|screenLayout"
orientation handle screen rotation in prehoneycomb devices and screenLayout for the rest.
So when you declare that in your activity element and rotate your device the onConfigurationChanged() method (that you should override) will be called and you can perform your operation inside it. The activity itself will not be recreate and onCreate() wont be called.

Android Pre-Load Splash Screen

Hopefully this isn't a dumb question, but when I initially click on my app, a kind of splash screen first appears for a few seconds with just a title bar indicating the name of the app and nothing else - then the app loads as expected. Is there any way to remove this screen?
The system looks into your AndroidManifest.xml to find out what is the theme of the main Activity. If you don't specify it manually, it assumes the application theme, if you don't specify application theme then the default theme is used. The system then creates the "splash screen" solely acoording to the theme.
So if you want to remove it, create a theme that has a transparent background and no ActionBar. And in onCreate() you probably want to change the theme again (or just change the background and show the ActionBar).
But it's better to have some visible "splash screen". Otherwise if you click on the icon, you won't see nothing for 1 - 2 seconds, which is quite weird, almost no app does this.
Initializing more resources on
onCreate()
may take some time. Try not to overload onCreate() method of Activity.
Try not to deep down more than 30 levels inside the xml layout file. which can also take up some time to initialize .
setContentView(R.layout.main)

Categories