If I use this code in my Manifest file:
<activity android:name=".MyAct"
android:label="#string/app_name"
android:configChanges="orientation|keyboardHidden"> //<-SEE THIS
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
As you can see I am declaring that the activity shall not be restarted when the screen rotation and keyboard visibility have been changed.
However, does this mean that the method onConfigurationChanged() will be called ONLY in case of these two events (in other cases the activity shall restart)?
Or it means that the activity does not restart even if only one attribute has been used?
I haven't been able to find this answer in the documentation.
Correct. It means that the activity does not restart even if only one attribute has been used. The onConfigurationChanged() method will be envoked if one of the attribute occur, i.e for those which are not specified, the activity will restart when they occur.
Related
I need the following problem to be resolved:
From the UnityPlayerNativeActivity I am starting a standard Android activity (may be mine, may be with an ad from the ad network - nevermind). When the game is being hidden with this activity on top (not the Unity one) by pressing the home button, as a user I have two ways of restoring it:
from the Recently used apps screen - the app is being restored to the same state, when it was minimized (that is what I expect to happen);
from the launcher, what causes the game's UnityPlayerNativeActivity is being restored with losing all other activities, that have been opened on top of it.
These activities are lost somehow (in a way I don't exactly know, what has happened with them). My game's logic depends on the result of the processes happening there, ie. I need to know, that this particular activity has been exited in any specific way (give a callback for example).
Do you know how I can bring this Unity activity back from launcher with all activities above it, as it was while minimizing it?
I want to understand the difference between the ways of restoring the app from Recently used screen and the launcher. I guess it is related to the intent-filter section within AndroidManifest.xml file, which is included within UnityPlayerNativeActivity entry.
The solution turned out to be quite simple, but not so obvious.
It has turned out, that the AndroidManifest.xml configuration that is being produced by Unity by default is causing this problem. For the main activity that is being started from launcher the following parameters are defined (taken from decompiled app):
<activity
android:alwaysRetainTaskState="true"
android:clearTaskOnLaunch="true"
android:configChanges="locale|fontScale|keyboard|keyboardHidden|mcc|mnc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|touchscreen|uiMode"
android:label="#string/app_name"
android:launchMode="singleTask"
android:name="com.unity3d.player.UnityPlayerNativeActivity"
android:screenOrientation="fullSensor"
launchMode="singleTask">
<meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="true"/>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
<category android:name="android.intent.category.LEANBACK_LAUNCHER"/>
</intent-filter>
</activity>
The problematic behaviour is being caused by these 2 parameters:
android:clearTaskOnLaunch="true"
android:launchMode="singleTask"
When the I change them to:
android:clearTaskOnLaunch="false"
android:launchMode="standard"
Then the app is resuming fine from the launcher.
The correct parameters should be set as follows:
<activity
android:alwaysRetainTaskState="true"
android:clearTaskOnLaunch="false"
android:configChanges="locale|fontScale|keyboard|keyboardHidden|mcc|mnc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|touchscreen|uiMode"
android:label="#string/app_name"
android:launchMode="standard"
android:name="com.unity3d.player.UnityPlayerNativeActivity"
android:screenOrientation="fullSensor"
launchMode="standard">
<meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="true"/>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
<category android:name="android.intent.category.LEANBACK_LAUNCHER"/>
</intent-filter>
</activity>
Note: While I am able to set the value android:clearTaskOnLaunch="false" for this activity explicitly, the launchMode parameter will be forced by Unity3d and set to "singleTask". I managed to change and check it by decompiling the app and rebuilding from these changed resources. I wonder if there is any elegant way to set this value.
You may find this blog post useful:
http://inthecheesefactory.com/blog/understand-android-activity-launchmode/en
I am narrowing down and explaining as simple as can,
main activity initiates an async task.
When orientation change onCreate() is recalled so Async is created once gain. so in onPreExecute() i lock my orientation and in onPostExecute() i release lock on orientation. By this way if Async task has started another instance of task will never get created.
another issue has started, in main activity itself findViewById() returns null when i randomly keep changing the screen orientation. Re-producing once in 5-6 tries.
how to go on this? any help
Does onCreate() get re-called after completion of method or main thread... or is it instantaneous as soon as orientation get changed
Thank you
---------------updated
<application
android:allowBackup="true"
android:icon="#drawable/ap_logo"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="agilepoint.android.mobilebpm.main.LoginActivity"
android:configChanges="orientation"
android:label="#string/app_name"
android:logo="#drawable/menu_button"
android:windowSoftInputMode="adjustPan|stateHidden"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<data android:scheme="http" />
</intent-filter>
</activity>
</application>
Found my solution...
android:configChanges="orientation" does not work with fragments
if you want to prevent runtime restarts due to orientation change when developing for API level 13 or higher (as declared by the minSdkVersion and targetSdkVersion attributes), you must include the screenSize value in addition to the orientation value. That is, you must declare android:configChanges="orientation|screenSize".
Add android:configChanges="orientation" for that perticular activity in manifest.
Is it possible to prevent an activity from becoming the root activity of a task? Can we mark it in the Manifest File?
UPDATE:
I have marked my activity's launch mode as android:launchMode="singleTask" to avoid multiple instances. What could be possible work-around in this case?
Just define that activity in android manifest file for eg.
<activity android:name=".MyActivity" />
Without giving any other attributes to it.
Actually, you do not need to do anything extra to prevent an activity to be the root activity. You just need to avoid adding following attributes while defining your activity in AndroidManifest.xml file.
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
My app seems to restart when it detects an orientation change. Well not really.
I only have portrait orientation enabled though.
In log cat I can see that my app is still doing calculations, but my progress dialog disappears.
What is going on?
In my manifest file this is my activity.
<activity
android:name=".xxxActivity"
android:label="#string/app_name"
android:configChanges="orientation"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Is there a specific reason why you're listening for the orientation change with android:configChanges="orientation"?
If not, remove that and I suspect your problem will go away.
add this right after the super call
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Better to change configChanges to
android:configChanges="orientation|keyboardHidden"
I guess, that activity is restarted, because you forgot to add onConfigurationChanged(Configuration) method in your activity.
http://developer.android.com/reference/android/app/Activity.html#onConfigurationChanged(android.content.res.Configuration)
It can be empty but shold be in the activity.
Also android:screenOrientation="portrait" is not needed.
my android application crashes and shows force close when i tilt the mobile phone. is there any suggestions that i can remove such problem? By the way i am developing a LBS application which uses the google maps (MapView).
I have a splash screen and then shown a ListActivity as below :
<activity android:name=".Splash" android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Home" android:label="#string/app_name" android:configChanges="orientation" android:windowSoftInputMode="stateHidden">
<intent-filter>
<action android:name="com.nepways.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
what is wrong with my declaration, the splash screen loads and list activity is also whown correctly but when i change the orientation the application is closed. Please help me.
When you change your orientation the activity is restarted
look at the logcat to see what exception causes your application to FC
You can change your manifest to disable activity restarting by customizing configChanges, if you stil have the problem override onConfigurationChanged() to fix what is generating this exception (like initialize something that may cause a nullpointer)
First of all as it is said the app is restarting (or as it is said in reference redrawing) each time when you change orientation.
To block it in your manifest file in activity you have to put this line which describes the activity orientation:
<activity android:name=".Splash"
android:label="#string/app_name"
android:screenOrientation="portrait">...
And about this buttons, or keymaps. Telling the truth Ive got thise warnings sometimes but it doesnt change anything in my app. So first change the orientation settings and then it should work correct.
If you want use other screen orientation remember to check here:
http://developer.android.com/guide/topics/manifest/activity-element.html
If you want to avoid restarting your activity on orientation change, you can put this in the activity in the manifest file:
android:configChanges="orientation"
Otherwise, we will need to see more code, and a stacktrace.