What does "android:configChanges" do? - android

I was creating an app that supports pip mode. I came across android:configChanges. What does it do?

In Android, when device configuration changes (such as users rotate the screen orientation, change language, etc..), the system will restart the running Activity (onDestroyed() is called, followed by onCreate()) to apply the new configuration.
If your application doesn't want this behavior to happen (maybe you don't want to restart the running activity or you want to apply the new configuration by yourself), then you can declare that the activity will handle the configuration by itself which preventing the system from restarting the activity.
To do that you go to the AndroidManifest.xml file, find the appropriate <activity> tag, add the android:configChanges attribute with a value that represents the configuration you want to handle.
Now when the configuration changes, the system does not restart your activity. Instead your activity will receive a call to onConfigurationChanged(Configuration) method along with the new configuration.
You should go to activity-element, read the "android:configChanges" section, you will understand it clearly. By the way, Android provides documentation about how to handle configuration changes in runtime here.

Related

Android do not scan NFC during device rotation

I have an application that uses NFC. When the Activity is created and enableForegroundDispatch() has been called, the NFC is scanned and accepted by my application. However, when the NFC is scanned DURING rotation (meaning the Activity has not been created yet, and enableForegroundDispatch() was not yet called), the default Android NFC scanner takes over displaying the "New tag collected" screen.
Is there any way to carry over the enableForegroundDispatch() even during device rotation? Or is there a way to temporarily "block" the default Android NFC capability when the device is being rotated?
Thanks
EDIT: The application only accepts the NFC scan when it is running. Scanning the NFC while the app is not running only displays a "New tag collected" screen
There's different possibilities:
Set screen orientation for your activity
<activity android:name=".MyActivity"
android:screenOrientation="portrait"
android:label="#string/app_name">
Tell android not restart activity when device rotation :
https://developer.android.com/guide/topics/resources/runtime-changes.html#HandlingTheChange
If your application doesn't need to update resources during a specific
configuration change and you have a performance limitation that
requires you to avoid the activity restart, then you can declare that
your activity handles the configuration change itself, which prevents
the system from restarting your activity.
<activity android:name=".MyActivity"
android:configChanges="orientation|keyboardHidden"
android:label="#string/app_name">
UPDATE 1
If you use applying android:configChanges. You have to manage yourself screen rotation as explained in doc :
Now, when one of these configurations change, MyActivity does not
restart. Instead, the MyActivity receives a call to
onConfigurationChanged(). This method is passed a Configuration object
that specifies the new device configuration. By reading fields in the
Configuration, you can determine the new configuration and make
appropriate changes by updating the resources used in your interface.
At the time this method is called, your activity's Resources object is
updated to return resources based on the new configuration, so you can
easily reset elements of your UI without the system restarting your
activity.

Excluding or overriding an AAR manifest

I am adding a library into my project. It comes packaged as an AAR. But it has a BroadcastReceiver that listens to a BOOT_COMPLETED starts a service. I don't want this behavior in my app. I would like it to start whenever the app starts. (This part is already done through the AAR, I assume in case the user force stopped the app).
Is there a way to either modify their manifest; or specify in Gradle not to merge their manifest (in which i'd have to declare their activities in my own manifest, I think); or to override their manifest and have a android:enabled="false for the BroadcastReceiver"?
I also would want to do it in the manifest or Gradle, because if I do it programmatically and if they install the app but never open it, the library service would start automatically next time the user reboots their phone.
You could re-declare their BroadcastReceiver in your manifest and use android:enabled="false" followed by tools:node = "replace".
Also if you later want to enable it, you can by using the setComponentEnabledSetting() method inside of PackageManager.
You can just open the AAR file with WinRAR or something else and edit the manifest file yourself. AARs are simply packaged ZIP files, just like APKs.
Indeed, Android Studio could handle this more gracefully, but I don't know if this is possible yet

What is the purpose of disabling an Android service in the manifest?

In Android it is possible to create a Service to do background tasks, etc by creating a subclass of Service. It order to use the Service it must be specified in the manifest for the app:
All services must be represented by elements in the manifest file. Any that are not declared there will not be seen by the system and will never be run.
One of the parameters for a Service in the manifest is the 'enabled' option:
Whether or not the service can be instantiated by the system — "true" if it can be, and "false" if not.
What is the purpose in declaring a Service to be disabled - if I didn't want the Service surely I just wouldn't write it / add it to the manifest in the first place?
The only use I can see for disabling a Service in the manifest, and it seems of limited value, is if it's a Service used only for debugging, and I want it disabled for production. Am I missing something?
The android:enabled attribute set to a boolean value defined in a resource file. The purpose of this attribute is to enable or disable the service on devices running on different Android OS version.
For example, to disable the service on devices running Android 4.3 or lower include menifest attribute android:enabled="#bool/atLeastKitKat".
In addition to including this attribute in the manifest, you need to do the following:
In your bool.xml resources file under res/values/, add this line:
<bool name="atLeastKitKat">false</bool>
In your bool.xml resources file under res/values-v19/, add this line:
<bool name="atLeastKitKat">true</bool>
if I didn't want the Service surely I just wouldn't write it / add it to the manifest in the first place?
In the very specific case of a Service, I agree that it would be rare for you to want to disable it. One possibility would be for a service that plugs into the system (e.g., input method editor, accessibility service) that you only want to enable at runtime (via PackageManager and setComponentEnabledSetting()) if the user make an in-app purchase that unlocks the feature. I am sure that there are other Service scenarios for this, though none are leaping to mind at this early hour of the day (yawn!).
However, I suspect that Service "inherits" its android:enabled setting by virtue of being one of the Android component types, along with activities, providers, and receivers. Other scenarios for android:enabled will be a bit more common with other component types. For example, it is considered good form to have your BOOT_COMPLETED receiver be disabled until you know that you need it. So, for example, if the BOOT_COMPLETED receiver is only used to resume a download interrupted by a reboot, you only need that receiver enabled if you are doing a download. At all other times, you may as well leave it disabled, so you don't waste the user's time during "normal" reboots.

Why do we mention activities in manifest?

We specify current activity and start activity for an intent and we call it through the method startService(). So why do we need to mention that activity in manifest again?
Indexing (what can your app do?)
I can answer at least part of the why for Activities. The manifest is also where you declare your IntentFilter which is how the system understands what your application does. i.e. should your activity be an illegible choice when the user is trying to take a picture? choose a file? share a piece of text? In addition to that the IntentFilter also tells the Launcher application that you would like to have your activity included in the Applications drawer.
Configuring (what is your main activity?)
There are also several configuration options that you can set on Activities which have to be done in the manifest i.e. SingleTop. Without the declaration in the manifest there would be no place to declare these configurations.
Time Saving (where can the system find your service?)
The manifest file is used by the system to know what kind of components do the application have. Without registering your Activities/Services/Receivers/Content Providers the system would have to scan and parse the whole apk every time someone wants to use a specific component to find it. This would be really slow, that's why there is the AndroidManifest.xml, which is a small file, and it can be parsed fast to find the required component.
Sources: Why do Activities/Services need to be explicitly added to the Android manifest? why acitivies have to be registered in manifest file
Each android application you build will include a file called
AndroidManifest.xml which is placed in the root of the project
hierarchy. So why is it important? Because it lets you define the
structure and metadata of your android application and its components.
http://simpledeveloper.com/android-application-manifest-file/

Android Safe Start Mode for Application

Is it possible to have a safe start mode for your Android Application. In the sense that the application will not start the main activity (which is intense) but will open another activity which will have tools to fix some of these problems.
My suggestion would be to create flag in shared preferences to store whether your app was closed properly or check something else you need. Then add one part before (activity or what you need) where you check this and decide about the mode you are getting in.
Hope this helps and enjoy your work.

Categories

Resources