In some cases I can see that Activity.onCreate is called before the Appication object gets created (before Application.onCreate is called). Is that ever possible?
May be you forgot to add your application class in manifest file.
Place your application class in AndroidManifest.xml class under <application> tag.
i.e.,
<application
android:name=".{YourApplicationClassName}"
...
...
In some cases I can see that Activity.onCreate is called before the
Appication object gets created (before Application.onCreate is
called).
This is not what Android document says about the Application class. As per the official android documents,
The Application class, or your subclass of the Application class, is
instantiated before any other class when the process for your
application/package is created.
Also below is specific explanation of onCreate() of an Application class
Called when the application is starting, before any activity, service,
or receiver objects (excluding content providers) have been created.
Hence the onCreate() of Application has to be invoked 1st and then onCreate() of Activity class
So the scenario you have mentioned is not possible as per the flow of instantiation of Application class and Activity class
In the case you are using logging to determine when application is created, check if you are using external logging system, like Timber which is usually instantiated in the end of application onCreate(). So it may appear that nothing before this system is instantiated when it is called.
Try to instantiate logging tool before super.onCreate() in application onCreate().
Related
I have a class in my app that extends the Application class. I have declared/named it in the Manifest.xml file. It basically initializes some stuff but more importantly gives me access to a serial port on the machine I'm programming on. Here is my question. I have an activity where I subclass this Application class. The Activity, lets call it Activity A initializes a variable that is declared in it's superclass (the one that subclasses Activity).
When I create an intent in Activity A meant to send the user to a different Activity, after calling startActivity(intent) I call finish(). Will that completely erase Activity A from memory, or, because in Activity A I reference a variable declared in the Application class, will this Activity A remain in memory
Nope, actually when we use superclass properties in sub class it won't be more in memory. So when ever we call for finish() for activity. All declared variables and all references related to that Activity will be destroyed (removed from memory).
I need to implement very specific code in the start of the application.
I mean, not in the start of the activity(onCreate() or onStart()) but in the start of the application.
I had one solution which is not good for me, which is to have a base activity called "MyBaseActivity" and then extends from it in all of my activities.
This solution is not good for me, because this solution makes me to be able to do only one specific thing in the onCreate of each activity(the specific code I talked about), which is not what I want.
I want every activity to be able to do different things according to their onCreate() func, and in addition to do the specific code that I talked about above.
Therefor, I need to access the start of the application, or that you have another solution for me.
Thank you !
The Application class, or your subclass of the Application class, is instantiated before any other class when the process for your application/package is created.
You need to extend application class.
public class AppApplication extends Application{
#Override
public void onCreate() {
super.onCreate();
//Do whatever you want
}
}
And this AppApplication class should be included in manifest file.
<application
android:allowBackup="true"
android:name=".AppApplication"
android:icon="#mipmap/ic_launcher"
I need to implement very specific code in the start of the application.
Every time when Android "gets a request" to start any of your app component (Activity, Service, BroadcastReceiver) and your app isn't running yet, it forks the app_process (a.k.a zygote), changes its name to your.package.name defined in AndroidManifest.xml, initializes an Application instance, calls its onCreate() method, then instantiates the component requested and calls its lifecycle methods (Activity's onCreate(), Service's onCreate() or BroadcastReceiver's onReceive()).
There can be only single instance of Application class which lives untill the app process dies. That said, any class instances you create within your extended Application class will also live until the app process is killed by the system.
Example: Understanding the Android Application Class
I'm extending the Application class for my additional custom need. And I'm calling a method inside that. As the expected behaviour, it is getting invoked for type of Android components(Activity, Service, Broadcast receiver, etc.,) But I want that too be invoked only on Activity. Is that any other way to overcome this problem ?
public class MyApplication extends Application {
#Override
public void onCreate() {
super.onCreate();
// the below method needs to invoked only for service
// but now called for all application components
myCustomMethod();
}
....
}
But I want that too be invoked only on Activity.
Can't be done. The Application instance will run if one component of your Application is open.
You need to do the customized stuff in another class and open it just when the instance of your desire component is open.
just add your code to onCreate method of your entry-point activity. If you want it to be called once per session - add two int keys to your shared preferences - app_launch_count and method_invoke_count. Increment first on App's onCreate and check the second in your Activity's onCreate if first greater then invoke the method :)
Move myCustomMethod() into the activity. An Application has no way of knowing what triggered the creation of its process.
Or, use registerActivityLifecycleCallbacks() on Application to register an ActivityLifecycleCallbacks object, and put your myCustomMethod() logic in onActivityCreated(). This requires a minSdkVersion of 14 or higher. That will tell you when each activity is created after your process is instantiated — if you only care about the first one, you would have to unregister the callbacks in your onActivityCreated() implementation.
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.
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.