Multiple Application name in Android Manifest - android

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!

Related

Extending the Application class in android twice

Is it okay or possible to extend the Application class in android twice and have two childs of it in an application? And also why do we need it & what's the purpose of extending it?
Is it okay or possible to extend the Application class in android twice and have two childs of it in an application?
You can only have one registered Application subclass in the android:name attribute of the <application> element. You are welcome to create as many subclasses of Application as you want, but only one will be used. However, you are welcome to have MyApp extend Application, have MyOtherApp extend MyApp, and register MyOtherApp in the manifest.
And also why do we need it
Few apps need it. Quoting the documentation, "There is normally no need to subclass Application".
what's the purpose of extending it?
If you have application logic that needs to be executed every time Android forks a process for you, Application is a common place to trigger that logic. For example, most crash logging libraries (e.g., ACRA) have you configure them in an Application subclass, so that they can handle crashes from everywhere else in your app, for every one of your processes.
You want something like this?
MyApp extends Application
MySecondApp extends MyApp
This is possible. There are several usecases for this. For example define an Application for your App but override it for a specific build type or product flavor (e.g. debug). Or a custom Application for your (Android) Tests:
MyApp extends Application
MyDebugApp extends MyApp
MyTestApp extends MyApp/MyDebugApp
Define each Application Class in the according AndroidManifest:
MyApp => main package
MyDebugApp => debug package
MyTestApp => androidTest package
Yes, it's possible. The reason extend classes is to reuse code written in the parent class and extend its functionality.

Can i use two classes that extends 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.

Class extending Application in Android project library?

I have a project (in Eclipse) which I've turned into an Android Project Library so as to re-use some of the code in another similar project. I think I've shot myself in the foot however as I'm getting the error:
Unable to start activity ComponentInfo{com.test.scroller1/com.lib.scrolltest.ScrollTestActivity}: java.lang.ClassCastException: android.app.Application cannot be cast to com.lib.scrolltest.resAppVars
com.lib.scrolltest is my Project Library which instantiates a class extending Application (resAppVars). In the onCreate() method I call:
mRav = (resAppVars) getApplicationContext ();
This way, I can use methods in the mRav object which would otherwise be a lot of duplicated code in other classes (such as passing a query to a generic select statement which returns an ArrayList of results).
What's the problem here? It seems I've hit a limitation in the way I've implemented the Application class.
Calling getApplicationContext() returns the Application object for the current application (i.e. the application that owns the activity that onCreate() is running inside of).
Unless you're doing something strange, you don't get to pick which Application class is used. There's even a note in the documentation for Application saying not to do this:
There is normally no need to subclass Application. In most situation,
static singletons can provide the same functionality in a more modular
way. If your singleton needs a global context (for example to register
broadcast receivers), the function to retrieve it can be given a
Context which internally uses Context.getApplicationContext() when
first constructing the singleton.
You should just create a regular shared class inside of your library project. Or if you don't have a need for the special functionality library projects offer, you can also just use a regular .jar file.
And if you need shared state, just make it a singleton. ;)
Although this is a very old post. I encountered the same problem and solved it. So I thought I'll post the solution for everyone.
It turns out that I forgot to declare the subclassed application name in the manifest file.
The android:name should point to the extended app class, even if it is defined in the referenced library.
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme"
android:name="com.example.lib.MyApp">
After I added that I could use the extended app with (<cast>) getApplication() anywhere in my project.

How to share resources between activities/services

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.

Can an activity based application have no activity?

Assuming I have a shared activity class defined in a Library project, which does not change for any application using it and thus does not need to be subclassed, can I get a way with creating applications without subclassing this activity for them?
To better explain my question, say I have a single activity in a Library project:
public class LibActivity extends Activity {
...
}
And now I am creating an application using that Library project. Do I really need to create
public class AppActivity extends LibActivity {
// totally empty!
}
Only so that the application have its own activity to be referenced in its own AndroidManifest.xml?
Can I get a way with a minimalistic approach, in which I subclass the activity only if I need to modify the library's activity core behavior?
Here is the fully qualified answer:
Yes, an activity based application doesn't have to derive an activity from the library's activity. The application simply uses the library's activity verbatim, unmodified.
Yes, I can get a way with a minimalistic approach, in which I subclass the activity only if I need to modify the library's activity core behavior.
I have been able to verify this with an AndroidManifest.xml that is identical in both the library and the application. It would be interesting to see whether some of this redundancy can be eliminated. I will experiment with this and report back.
UPDATE: Sure enough, it is possible to create a perfectly running application in which the only activity is defined in the library and the library's AndroidManifest.xml doesn't have any <application> or <activity>! This is possible if the application's AndroidManifest.xml has them.
You can reference library Activity classes directly from your application AndroidManifest.xml. Just specify the fully qualified name like so android:name="com.example.LibActivity"

Categories

Resources