I'd like to share some data between them that is not suitable for intents or serialized data files. I know I can use the overall Application for this purpose, but I'm not fully understanding how it works I'd just use static data members as a cache for your persistent store, rather than fussing around with Application Application gives you little advantage over static data members and has one big cost: there can only be one Application whereas you can organize your static data members/singletons however you like I would expect that once the app begins from a cold start, it first instantiates and initializes MyApplication (calling onCreate()) and then goes on to instantiate and start my activity (MyActivity) (which was marked with the intent android.intent.action. MAIN).
The logs seem to bear this out Correct But... if I try to set the following Activity-wide variable, it gets set to null: private final MyApplication THISAPP = (MyApplication)getApplication() Of course. The activity object has not yet been initialized. You are welcome to use initializers like this for constants or things you get from static methods (e.
G Calendar.getInstance() ) or other constructors (e. G new ArrayList() ). Do not call a superclass method an expect it to work at this point, since the constructor chain has not yet been called on the object being instantiated.
This is just Java, nothing particular to Android.
I'd like to share some data between them that is not suitable for intents or serialized data files. I know I can use the overall Application for this purpose, but I'm not fully understanding how it works. I'd just use static data members as a cache for your persistent store, rather than fussing around with Application.
Application gives you little advantage over static data members and has one big cost: there can only be one Application, whereas you can organize your static data members/singletons however you like. I would expect that once the app begins from a cold start, it first instantiates and initializes MyApplication (calling onCreate()) and then goes on to instantiate and start my activity (MyActivity) (which was marked with the intent android.intent.action. MAIN).
The logs seem to bear this out. Correct. But... if I try to set the following Activity-wide variable, it gets set to null: private final MyApplication THISAPP = (MyApplication)getApplication(); Of course.
The activity object has not yet been initialized. You are welcome to use initializers like this for constants or things you get from static methods (e.g. , Calendar.getInstance()) or other constructors (e.g. , new ArrayList()). Do not call a superclass method an expect it to work at this point, since the constructor chain has not yet been called on the object being instantiated.
This is just Java, nothing particular to Android.
Fair enough; now that I see this is related to the constructor chain then it makes more sense. I'm not yet sure how I'd access static static data members readily (I assume you mean public members of the respective Activities, or do you mean something else? ) I have no problem using the Application object if the only "disadvantage" is that it's global (it seems to be an advantage here, in terms of data visibility... I'm not touting globals as virtuous, but they'll be FAR less cumbersome for passing complex data between Activities.) – MartyMacGyver Oct 16 at 18:00 @MartyMacGyver: If it is complex, either it should be part of your data model (in which case, a static cache backed by persistent storage is one strategy), or it should not be passed between activities (e.g. , steps in a wizard should all be part of one activity).
I can think of no scenario in which data that is "complex" should be eligible for loss because the user navigates away from your app and your process gets terminated. – CommonsWare Oct 16 at 18:52.
Created a class that implements Application public class MySuperApplication extends Application { public String SomeSetting; } Defined which is your Application class in manifest (important! ) MySuperApplication" android:icon="@drawable/icon" and then accessed it in your activities app = (MySuperApplication) getApplication(); app. SomeSetting = "Test"; This should work.
It does in my case.
Yep... as stated originally, I can do that (before I posted the question I also left the ". " out of the android:name designation... a subtle but major mistake! ) Still, I cannot make THISAPP a final variable (getApplication() appears to be invalid before onCreate() is called) and I'm not seeing why that should be.
– MartyMacGyver Oct 16 at 17:29.
AFAIK, You cannot declare a empty final variable and initialize it onCreate. In java it is initialized during declaring the variable or in the constructor. THISAPP cannot be initialized to Application object during declaration because the Application doesn't exist during compile time.
Because onCreate is not a constructor, you cannot declare a empty final variable and initialize it in onCreate.
I figured that out - it was quickly clear that initializing anywhere other than the definition and the constructor was not going to work (since normally I only do that in the definition it'd been a while since I bumped into it... and since you can't override Activity constructors it was clear that the only place I can do it was the one place it wouldn't work. ) – MartyMacGyver Oct 16 at 18:03.
Instance variables are created when the object (in this case an Activity object) is constructed. For getApplication() to produce a valid value then the Activity object would have to have been provided enough context (little 'c') at instantiation for this to occur. The source for getApplication() in Activity.
Java is just this: /** Return the application that owns this activity. */ public final Application getApplication() { return mApplication; } Here is where it is set: final void attach(Context context, ActivityThread aThread, Instrumentation instr, IBinder token, Application application, Intent intent, ActivityInfo info, CharSequence title, Activity parent, String id, Object lastNonConfigurationInstance, Configuration config) { // other code mApplication = application; // other code } Since mApplication is not set until after attach is called, it won't be available to getApplication() on instantiation of your Activity.
Ah... that's interesting and more detailed than what I've found elsewhere. At what point in the process does that attach() actually happen? – MartyMacGyver Oct 16 at 19:16.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.