How to create Application class? - android

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.

Related

Related to Activity addition to manifest in Android

Is it compulsory to add a class file, which extends activity, to manifest?
I have created a java file for handling file IO and to use openfileoutput() openfileinput() i have extended Activity class.
YES you most declare it in the manifest so the application will know its activity.
If you use/call that class (which extends Activity) somewhere in your code, you MUST declare that class (Activity) in Manifest file. If you don't, you will get exception.
Manifest file is used to declared essential information for your app (Activity, Service, Broadcast Receiver, keys,...).
For more info about Manifest, you can check it here.
when you launch the application first the OS will check your manifest file.So if your Activity is not Listed in the Manifest file.Then it won't execute it.So your ACTIVITY class should be declared in the Manifest file.

application's name attribute - What is it for?

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

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