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.
Related
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
{
PROBLEM: Need to initialize some objects before an activity from secondary (non-base) Instant App feature is launched.
RESEARCH: In base feature we can create android.app.Application class and declare it in the base features's Manifest:
<application
android:name="com.example.MyApplicationOfBaseFeature">
As a result, android.app.Application.onCreate() method will be called before any other activity's onCreate().
But adding the same to secondary feature results in Manifest merge error:
<application
android:name="com.example.MyApplicationOfSecondaryFeature">
QUESTION: How do I do the same for a secondary feature in my Instant App?
I am new to volley.I have two classes,AppController Singleton class, and ImageController Singleton class.But in Manifest,it allows only one application name.So how do I solve this?
You can try this way:
Application Class no one:
public class MyApplication extends Application {
}
Application class no two:
public class MyApplication2 extends MyApplication {
}
In your Manifest:
<application
android:name=".MyApplication2"
android:allowBackup="false"
android:icon="#drawable/app_icon"
android:label="#string/app_name"
android:supportsRtl="true"/>
Only the <manifest> and <application> elements are required, they each must be present and can occur only once
According to documentation manifest file with only one application element is valid.
Try to Marge in Single Application Class
First of all, every application should have only one application class that is the concept of MVC in android.
You should remove one application class and do whatever it is doing in another one.
As per your requirement: you have AppController and ImageController application class then remove all the code of ImageController application and merge it in AppController application class. Now use AppController class in manifest.
I'm guessing you are using a Singleton as to have a class that holds your App data for the current session and\or another one for specific stuff (e.g. networking), if so, you have a couple ways of going about that:
Use ONE Application class - you shouldn't have more than one, if so - merge them.
If you insist on having 2 Singleton classes because you want to separate some functionality, you may create 2 Singleton classes, which are NOT your application class.
If you choose option 2, you should initialize(and maybe also control) them from your application class, especially to avoid duplicating a context object (that might lead to a memory leak), but make sure it's really necessary first.
since you tagged your question with the Volley tag, I'm guessing this SO thread about isolating Volley requests might help.
Hope anything here helped!
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".....
I wanted to know what is the best approach for sharing singleton in my android app.
I have a few activities that does not rely on each other, but I want them to have a singleton that they can all read/write.
Where should I place it?
Thanks
Your application should be a singleton. It avoids many other problem related to sharing datas or models.
In your manifest.xml :
<application android:icon="#drawable/icon"
android:label="#string/app_name" android:debuggable="false"
android:name="<class of your application see below>"
android:launchMode="singleTop">
..... all activities/services/receivers/etc... as usual
</application>
And your android:name refers to this class :
public class MyApp extends Application {
//singleton, but use onCreate more than constructor to build shared resources.
....
}//class
And then, from within any activity, you can retrieve your singleton to get shared resources by doing this :
((MyApp)getApplicationContext()).getSharedResources()....
or
MyApp.getInstance().getSharedResources()....
Having a singleton as a app is a usual design pattern in android, it is especially useful to handle device rotation (as it tends to destroy activities and recreate them) as it provides a stable entity to persist during app lifecycle.