I am using
Android global variable
example.
How do I add another "application" tag in AndroidManifest.xml ?
According to the documentation, only one application tag can be inserted in the manifest.
Quote from doc:
"Only the manifest and application elements are required, they each must be present and can occur only once."
If you're following the example at the given link, just add the android:name and android:label XML element to your current application tag and it'll work just fine.
Example of my application tag in an application I'm developing at the moment:
<application
android:name=".Globals"
android:debuggable="true"
android:icon="#drawable/icon"
android:label="#string/app_name" >
Globals is my application class.
I know this question was asked a long time ago but using Android Studio 3.0 and higher, I came across the same problem and did not find anything online that helped.
After a lot of research and my own testing, I found out how to successfully implement two or more classes that both extend android.app.Application into the Manifest's <Application/>.
First Class File:
public abstract class MyClass1 extends Application {
// ...
}
Second Class File:
public class MyClass2 extends MyClass1 {
// ...
}
Manifest File:
<application
android:name="com.mydomain.myapp.MyClass2">
<!-- Rest of code here... -->
</application>
how to handle multiple application classes in android
When a library is already extending the "Applicaton" class just do the following
class lib1 extend Application {
...
}
class lib2 extend lib1{
...
}
in the manifest -> <application android:name = "package.lib2".....
Related
I'm new to Android app dev, and I'm trying to use a library (LocaleHelper) that says to extend my App class:
class App : LocaleAwareApp() {
}
And to extend my base activity class:
open class BaseActivity : LocaleAwareCompatActivity() {
}
but I don't have either of these in my Android Kotlin project. All I see for source code generated is the MainActivity class.
I'm using Android Studio 3.4.2 with min sdk set to 22. Is there a different way to integrate this library?
You have to create a new App class like this:
class App : LocaleAwareApp() {
}
and in AndroidManifest add this line inside the application tag:
android:name=".App"
After that you create a BaseActivityClass:
open class BaseActivity : LocaleAwareCompatActivity() {
}
and your MainActivity and every other activity you create should inherit from this BaseActivity like this:
class MainActivity: BaseActivity() {
}
For more customization you should look into https://github.com/zeugma-solutions/locale-helper-android
App (I assume you mean Application) and Activity classes are part of the std android library, so you don't have to "have them". Extending them means you have to write your own implementation with "extends Activity" and "extends Application" in class prototype.
more info here: https://medium.com/#balakrishnanpt/android-application-class-a8a1d64c82d1
hope that helps
Assuming that you mean this library, LocaleAwareApplication is a class in the library. You would create a subclass of it in your project:
class MyAwesomeApplication : LocaleAwareApplication() {
}
Then, in your manifest, in the <application> tag, add an android:name attribute that points to your subclass:
<application android:name=".MyAwesomeApplication"
// other attributes go here
>
I do not know why the README has the wrong class name (LocaleAwareApp instead of LocaleAwareApplication).
Android application using Xamarin but I think this applies to non-Xamarin. I read this article while doing research.
In an Android application, you will notice that the AndroidManifest.xml file contains an entry for which is used to configure various properties of your application: icon, label, theme, etc. Depending on your application's needs, this is sufficient. The application when instantiated will perform the necessary initializations based on the values configured in the manifest, and it will then load the Activity that is defined as the view to load on startup.
In some cases, you may wish to extend the Application class in order to perform custom initialization on application startup, or to maintain global application state (for example, in the case of a game that is tracking score across multiple levels). The example that you have shown above is the case where someone has decided to subclass the Application class and override it's OnCreate method, although they haven't included any custom code in the override method. It looks like their purpose was to create a global static property to always have access to the Activity that is currently loaded.
Previously, I did not need to extend the application class so I just had my manifest handle it like this article says. However, I recently had a need come up to extend the application class so I can use the OnCreate event for the application.
I added a new class which extends Application and it looks like this:
namespace MyApp.Droid
{
public class MyApp : Application
{
public MyApp(IntPtr handle, global::Android.Runtime.JniHandleOwnership transfer)
:base(handle, transfer)
{
}
public override void OnCreate()
{
base.OnCreate();
//Do OnCreate things here.
}
}
}
It seems just adding this class is not complete. First question, do I also have to modify my manifest now? Second question, the class can have attributes for theme, icon, etc... which are and have been defined in my manifest. Can they stay in the manifest or do I need to move them to this new class as attributes?
Here is the manifest:
<application android:allowBackup="true" android:icon="#drawable/mainapplogo" android:label="#string/app_name" android:roundIcon="#mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="#style/AppTheme">
Thank you!
First question, do I also have to modify my manifest now?
In this question if you mean just because you have added the new application class is there a need to make any change in the existing manifest then NO there is no such requirement.
Second question, the class can have attributes for the theme, icon, etc... which are and have been defined in my manifest. Can they stay in the manifest or do I need to move them to this new class as attributes?
The Android Manifest's Themes and everything else will stay there, The Android Application class and AndroidManifest.xml work independently and hence adding an extended application class does not require any change in your Manifest file or vice versa.
Good luck,
Feel free to revert in case of queries
UPDATE
See to it that your application class has the [Application] attribute on top of the class name
#if DEBUG
[Application(Debuggable=true)]
#else
[Application(Debuggable = false)]
#endif
public class MyApp: Application
{
This guide here tell you how to create an Application class:
http://www.intridea.com/blog/2011/5/24/how-to-use-application-object-of-android
It mentions that you need to define the name of your Application class in your manifest:
<application android:icon="#drawable/icon" android:label="#string/app_name" android:name="MyApplication">
I think Application class are useful to hold static variables / objects like for example, GoogleApiClient so that you do not have to reconnect with Google each time your activity ends, instead if you put it in your application class, you will only need to connect once when your app starts up and disconnect when it closes.
I have a multidex app: https://developer.android.com/tools/building/multidex.html
Multidex apps requires me to also give a constant name of "android.support.multidex.MultiDexApplication" to the application in the manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.multidex.myapplication">
<application
...
android:name="android.support.multidex.MultiDexApplication">
...
</application>
</manifest>
If I have to give android:name="android.support.multidex.MultiDexApplication" as the application name, I cannot also give android:name="MyApplication". As far as I know, an application cannot have two names.
Is it possible to have both multidex and a custom application class in an app?
I think Application class are useful to hold static variables / objects
If they are static, then Application is not holding them.
Is it possible to have both multidex and a custom application class in an app?
According to the documentation, you can either have your class extend MultiDexApplication or have it override attachBaseContext() and call MultiDex.install(this) instead of needing MultiDexApplication.
Ref: Android:- How to Make dynamic HashMap Accessible in whole application?
I have set up a CustomHashMap (just a collection of 3 LinkedHashMaps) so that I can access it from 2 Activities, but I am
unable to access it from the SecondActivity! The only way to remove red errors in Manifest.xml is to set the lines below. If I
use android:name="com.example.intents.CustomHashMap" (following the online example) I get an error saying 'is not an enclosing
class'.
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher_icon"
android:label="#string/app_name"
android:theme="#style/CustomActionBarTheme"
android:name="com.example.intents.MainActivity$CustomHashMap"> //--- The system added the $ parts here.
<activity
....
protected CustomHashMap thisMap;
....
thisMap = (MainActivity.CustomHashMap)getApplication();
In MainActivity I can do things like thisMap.getKeyValue(4) but from SecondActivity only null is returned. I feel as if I am
missing something very simple here. Any ideas?
As see here android:name :
The fully qualified name of an Application subclass implemented for the application
Means need to pass class name which extends Application instead of any other component name like Activity,Service,...
See following tutorial for adding Application class in application :
Global Variable Or Application Context Variable - Android Example
I'm kind of a noob at programming for the Android OS. I noticed in the books I have been reading that the authors have placed a "dot" in front of the activity name when registering their activities in the manifest. I've looked around the Android developer site and I can't figure out why we need the "dot". Does the "dot" actually server a purpose? Do I need it? I have included an example below. Notice the "dot" before "NewActivity":
<activity android:name=".NewActivity"></activity>
As you have noticed the point is not necessary but it basically means: the activity class lives in the same package of the app. So, if your app package is: com.my.package then:
.YourActivity means that your class is inside com.my.package.
YourActivity means that your class is inside com.my.package (same as above).
.activities.YourActivity means that your class is inside com.my.package.activitites.
You can even do something like: com.my.package.activities.YourActivity which is useful when you want to have different versions of your app and use Ant to change the references to the package automatically.
http://developer.android.com/guide/topics/manifest/activity-element.html#nm
android:name
The name of the class that implements the activity, a subclass of Activity. The attribute value should be a fully qualified class name (such as, "com.example.project.ExtracurricularActivity"). However, as a shorthand, if the first character of the name is a period (for example, ".ExtracurricularActivity"), it is appended to the package name specified in the <manifest>.
So given ApplicationManifest.xml:
<manifest
...
package="com.stackoverflow.android.geotask"
...>
<application ...>
<activity android:name=".view.TaskListListView" ...>
...
</application>
</manifest>
then since android:name=".view.TaskListListView" has a leading period, so it is interpreted as android:name="com.stackoverflow.android.geotask.view.TaskListListView".
That dot will append your package in your application manifest.
If your package name is com.app.demo.
<activity android:name=".HelloWorldActivity">
It means that Activity is lying inside demo package.
You can replace this with
<activity android:name="com.app.demo.HelloWorldActivity">