application's name attribute - What is it for? - android

Inside my android manifest file I have an application tag and inside is name property/label.For what is this used for ?

If you are referring to the android:name XML attribute, then, quoting the documentation, it is:
The fully qualified name of an Application subclass implemented for the application. When the application process is started, this class is instantiated before any of the application's components.
The subclass is optional; most applications won't need one. In the absence of a subclass, Android uses an instance of the base Application class.

Application class in the Android is used for global settings or running entrance.
In the Android reference it describes the Application class: "Base class for those who need to maintain global application state. You can provide your own implementation by specifying its name in your AndroidManifest.xml's tag, which will cause that class to be instantiated for you when the process for your application/package is created." more

Related

android - Difference between android:name and android:label

I am flutter developer and some Android settings confuses me.
What is the difference between android:label and android:name in AndroidManifest.xml??
<application
android:name="io.flutter.app.FlutterApplication"
android:label="flutterapp2"
android:icon="#mipmap/ic_launcher">
The
android:name="io.flutter.app.FlutterApplication"
is a default to a Flutter application & you should NOT edit this anyway (unless you created a class that extends FlutterApplication class).
The android:label is to define your app name, which is display in the installed application list.
If you want to change the app name in home screen, check android:label inside the <activity /> tag
For more information, check out the official documentation:
https://developer.android.com/guide/topics/manifest/application-element
android:name
The fully qualified name of an Application subclass
implemented for the application. When the application process is
started, this class is instantiated before any of the application's
components. The subclass is optional; most applications won't need
one. In the absence of a subclass, Android uses an instance of the
base Application class.
android:label
A user-readable label for the application as a whole, and a default
label for each of the application's components. See the individual
label attributes for , , ,
, and elements. The label should be set as a
reference to a string resource, so that it can be localized like other
strings in the user interface. However, as a convenience while you're
developing the application, it can also be set as a raw string.
According to https://developer.android.com/guide/topics/manifest/application-element
android:name
The fully qualified name of an Application subclass implemented for the application. When the application process is started, this
class is instantiated before any of the application's components.
The subclass is optional; most applications won't need one. In the absence of a subclass, Android uses an instance of the base
Application class.
android:label
A user-readable label for the application as a whole, and a default label for each of the application's components. See the
individual label attributes for , ,
, , and elements.
The label should be set as a reference to a string resource, so that it can be localized like other strings in the user interface.
However, as a convenience while you're developing the application, it
can also be set as a raw string.
android:name this is a class which will wxecute first time (this is not your app name ,this is a functional thing)
and
android:label this is name of app shown on icon which represent the app name
android name is the name of the package that you are defining for that project and android label is the default name for your application
The android documentation states the following and you may find the list of attributes here
android:name
The fully qualified name of an Application subclass implemented for the application. When the application process is started, this class is instantiated before any of the application's components.
The subclass is optional; most applications won't need one. In the absence of a subclass, Android uses an instance of the base Application class.
android:label
A user-readable label for the application as a whole, and a default label for each of the application's components. See the individual label attributes for , , , , and elements.
The label should be set as a reference to a string resource, so that it can be localized like other strings in the user interface. However, as a convenience while you're developing the application, it can also be set as a raw string.

How to create Application class?

In this artical is this
Add your Parse API keys
Create or open your Application class, and add the following line to
your onCreate method:
Parse.initialize(this, "YOUR_APP_ID", "YOUR_CLIENT_KEY"); Make sure to
replace "YOUR_APP_ID" and "YOUR_CLIENT_KEY" with your Parse app's id
and client key. You can find these in your app's Settings page.
how to create and implement it?
Quoting the documentation for android.app.Application:
You can provide your own implementation by specifying its name in your AndroidManifest.xml's tag, which will cause that class to be instantiated for you when the process for your application/package is created.
Specifically, the class name of your Application subclass would go in the android:name attribute of the <application> element.

Android Application sub-class constructor not being called

I have a class in my Android application that sub-classes the AndroidApplication object. The documents say:
public void onCreate ()
Since: API Level 1 Called when the
application is starting, before any
other application objects have been
created. Implementations should be as
quick as possible (for example using
lazy initialization of state) since
the time spent in this function
directly impacts the performance of
starting the first activity, service,
or receiver in a process. If you
override this method, be sure to
call super.onCreate().
I placed a breakpoint on my sub-class's constructor and when I run my application, it is never reached. Naturally, when I call the sub-class's getInstance() method from other code it returns NULL since the instance variable is (supposed to be) initialized when the constructor is called.
Can anyone tell me what is wrong? I would assume from the docs that I don't have to create an instance of the AndroidApplication sub-class myself, or do I? Am I supposed to modify my manifest file somehow to add the AndroidApplication sub-class and if so, how?
-- roschler
I'm posting the answer here for others. Yes you need to add the name of your Application object sub-class's name to the Android manifest. For Eclipse users, the easiest way to do this is to open the AndroidManifest.xml file, select the Application tab in the manifest editor, and use the Browse button next to the Name field to find your Android Application object sub-class name and select it. The manifest file will be updated properly to register it. I just did that and it worked.
I had a problem of not having a . before my application class name.
Should be:
android:name=".MyApp"
since the MyApp class is in the package defined in the manifest.

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"

Android: this.getApplication() returns NULL pointer

I'm running the following line in an Activity, which is within the same application, but in a different package:
AppObject appObj = (AppObject)this.getApplication();
// FYI: AppObject is my extension class of Application.
It returns only a null pointer, while when I move it to the "main" package and run it from there it returns the application reference as expected.
I've defined the activity in my AndroidManifest.xml with the full qualified class name, since it is in another package: <activity android:name="com.foo.bar.TestActivity"></activity>
Update: As suggested in a question below android:name="AppObject" was already in the <application> tag of the AndroidManifest.xml
It's important to call getApplication() in the activity's onCreate() method, not in the constructor.
You need the update application tag to AndroidManifest.xml with your class name, which is extended from Application, with proper package name.
<application android:name=".AppObject">
As per Application tag google docs, Base class for those who need to maintain global application state. You can provide your own implementation by specifying its name in your AndroidManifest.xml's tag, which will cause that class to be instantiated for you when the process for your application/package is created.
here
Just run into the same thing, having refactored all my code still got the same issue, noticed that I was setting the local mApplication variable in the constructor, it should go in the onCreate(), I think all the objects in the manifest may be constructed first before getApplication() is setup so you need to call getApplication() in or after onCreate() has been called. Haven't refactored all my code back again to see if this works for different packages (sigh).
I think, it isn't a null pointer, but your function which you want to use next in the class AppObject maybe wrong.

Categories

Resources