Skip to content Skip to sidebar Skip to footer

Ok To Use Private Constructor In Activity So I Can Use Final Modifier On Variables?

The entire original question was based on a mistake. You can NEVER assign the value of a final static FIELD constant in the constructor. If you could, you would have the value of

Solution 1:

To basically answer the main part of your question title...

OK to use private constructor in Activity...?

No.

I'm assuming you're coming to Android from a previous Java stand-point but one thing to understand about Android is that the Activity class is one of a few special case classes which should be left to work as they're designed to.

Don't concern yourself with the concept of constructors when it comes to the Activity class just follow the design methodologies and application fundamentals which work quite adequately.

If you have a specific requirement which doesn't work within the Android framework then post a question with example code and somebody will explain how to fix it.

EDIT: In light of your update to your question...

publicfinalstatic MyActivity uI;

With this in place, I can refer back to the "user interface" object as MyActivity.uI without having to pass references through chains of calls.

Seriously...don't do that. There are perfectly adequate, functional and safe mechanisms which can be used to easily handle any situation with minimal code without 'breaking' the model in the way you're attempting to do things.

To repeat what I said above in my original answer - the Android Activity class is a special case. Instances of Activity and their methods/fields are not meant to be accessed directly by any other external class.

You'll be doing yourself a favour and save a lot of headaches if you simply accept the fundamentals of the Android framework. Coding Android apps may well involve Java but not all 'generic' Java concepts apply, in particular to some of the core components such as Activity.

Solution 2:

Since the activity manager instanciates your Acitivity you probably don't get your constructor called - not sure if that works with a private contructor + final.

Also several things can't be used at construction time since your activity is not completely initialized until onCreate is called (like SharedPreferences).

Solution 3:

Fine, your constructor might be getting called. However, there's no "single" activity (not really), so setting a final variable that references an instance of your activity is NOT the right way to go about this. If you support orientation changes, for example, the activity will get recreated (as you have found out), and your final reference is wrong. Also, since you have a reference to a destroyed activity (that is no longer used), you've just leaked that activity. I'm afraid you're gonna have to go through the normal methods of creating and instantiating views.

Anyway, I think a better question is an example of what you're trying to accomplish (and why you found it so troublesome), rather than trying to side-step what's already in place.

Post a Comment for "Ok To Use Private Constructor In Activity So I Can Use Final Modifier On Variables?"