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
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 am instantiating an application object in the manifest file as below.
<application android:name=".MyApp"
android:icon="#drawable/add_icon_with_oval"
android:label="#string/app_name"
android:process="com.test.myprocess"
android:allowBackup="false">
I am starting a service from my application which is running in another process. This service is also declared in my manifest file as below.
<service
android:name="com.test.package.MyService"
android:permission="some.permission"
android:process="com.test.anotherprocess" >
</service>
The problem that I am seeing here is that MyApp is instantiated twice, one for my process, com.test.myprocess and another instance for com.test.anotherprocess.
Is there is a way that I can prevent creating MyApp for com.test.anotherprocess
Since the service is a component of application, it can't run without an application. The only way to have a single instance of your Application object is to remove the android:process attribute from your service.
For more details, check: http://developer.android.com/guide/topics/manifest/service-element.html#proc
Is there is a way that I can prevent creating MyApp for com.test.anotherprocess
you cannot, system will will create Application instance for each process. If you have some initialization code in your custom Application class then you can check process name and for example skip it, see code here: Is there a way to get current process name in Android.
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".....
What does the dot mean?
most of the time I just write that:
<activity android:name="OneActivity" ...>...</activity>
But sometimes I see in the autogenerated file:
<activity android:name=".OtherActivity" ...>...</activity>
And also in the Docs for Service I see that they write
<manifest ... >
...
<application ... >
<service android:name=".ExampleService" />
...
</application>
</manifest>
But I never saw any differences in trying the one or the other.
If you take a look at above, there is package definition like
package="app.package.name"
The class name with the dot means that that class is under the defined package.
If you have another package like
app.package.name.another
and there is a class in that package, you have to defined class name like
<activity android:name=".another.activityname"
From the the Android Dev Guide < activity > reference
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 element. There is
no default. The name must be specified.
Credit: jaywon from Activity Name in android Manifest in Stack Overflow
You can find some differences if you create more than one package, android checks for the class in the default folder that you might have mentioned while creating project.
As for the service, it automatically appends "services" to the package name and looks for the service in it. so its more like relative and absolute paths, if you place your service in different package name, you will have to mention the whole package path with class name. It applies to receivers too..
For picking up any activity Android requires fully qualified name...
For that Our Manifest files has attribute (i.e. package="com.test")
So to make it fully qualified we put dot before activity name (i.e. android:name=".FirstActivity" )
If you do not want to use dot before every activity then simply put dot after your package attribute in manifest tag.(i.e. package="com.test. ") and write activity name without dot (i.e. android:name="FirstActivity" )
So that it can overall make a fully qualified name (i.e. com.test.FirstActivity)
The dot before the name means that it is a hidden file that will not be seen by others. You can see on youtube how to hide files by android cell.
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">