I am developing a native android application, in which I am trying to use 2 open-source libraries. Problem is both the libraries are using Application Class in their respective libraries. They are registering these classes in their respective source code in manifest file using "android:name" under the application tag.
Question is how to handle such a scenario, since as we know, only ONE tag can be used inside manifest file.
Can we register/instantiate the Application Class in the code, so that we mention only ONE library in tag and the second using code/pragmatically.
OR are there any other alternatives.
Please share your comments/suggestions.
Thanks in advance.
You need to implement Multilevel inheritance to resolve this scenario.
This is your scenario
public Lib1Application extends Application{
}
public Lib2Application extends Application{
}
public YourApplication extends Application{
}
How to resolve this?
public Lib1Application extends Application{
}
public Lib2Application extends Lib1Application{
}
public YourApplication extends Lib2Application{
}
finally in mainfest.xml
<application
android:name="com.your.packagename.YourApplication"
android:icon="#drawable/ijoomer_luncher_icon"
android:label="#string/app_name"
>
Only the manifest and application elements are required, they each must be present and can occur only once. Most of the others can occur many times or not at all — although at least some of them must be present for the manifest to accomplish anything meaningful.
See this link:
http://developer.android.com/guide/topics/manifest/manifest-intro.html#filec
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
{
I have MyApplication.java class that extends Application but when i use a library .aar that also have a Global.Java class extends Application.
I have update my menifest file with
<application
android:name=".activity.MyApplication"
But have got following error:
Caused by: java.lang.ClassCastException: com.xxx.xxx.activity.MyApplication cannot be cast to com.xxx.utils.GlobalClass
Is there anyone who face the same?
please help.
According to documentation manifest file with only one application element is valid.
Only the and elements are required, they each
must be present and can occur only once.
Even if you have two classes which extends from application you can only specify one of them under the application tag. Hence answer to your original question, you cannot have two application classes actually.
To solve your problem you can extend from GlobalClass, your problem would be solved because Global class is inheriting from Application and you are inheriting from GlobalClass.
MyApplication IS-A GlobalClass
GlobalClass IS-A Application
Hence MyApplication IS-A Application.
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 having two classes in my android project so, should I mention two activity tags in my app manifest file or will it work without including the activity tag.
Although, my app is working fine but I think this creates error - Unfortunately, your app has stopped working.
it depends Which kind of classes you have.. if your class is extending Activityor ActionBarActivity or AppCompatActivity or the class wich extends any of these, than you must define it in manifests.xml file. otherwise if your class extends nothing or just extends view, then no need to define it in manifests.xml file.
Yes,every Activity you used must be declared in manifests.xml,or when jump the activity,app will crash.
My Android application requires two application classes,one is volley AppController and another one is Analytics class.I am confused how to add both at a time.please help me and get me out of this.
You need to implement Multilevel inheritance to resolve this scenario.
This is your scenario
public Lib1Application extends Application{
}
public Lib2Application extends Application{
}
public YourApplication extends Application{
}
How to resolve this?
public Lib1Application extends Application{
}
public Lib2Application extends Lib1Application{
}
public YourApplication extends Lib2Application{
}
finally in mainfest.xml
<application
android:name="com.your.packagename.YourApplication"
android:icon="#drawable/ijoomer_luncher_icon"
android:label="#string/app_name"
>
Make your volley AppController class extend your Analytics application class. Then specify your volley AppController in your manifest.
You need to implement Multilevel inheritance to resolve this scenario.
how to handle multiple application classes in android