Android - Activity Constructor VS Oncreate

Android - Activity Constructor vs onCreate

I can't think of any good reason to do anything in the constructor. You never construct an activity directly, so you can't use it to pass in parameters. Generally, just do things in onCreate.

Custom Application class: Constructor vs. onCreate()

I assumed that MyApplication.onCreate() would run before all other code? Isn't that correct?

ContentProvider instances are created before onCreate() is called on the Application. In theory, your stack trace should show you what code of yours is being invoked prior to your initialization.

Is it save to move someCustomInit(); to the constructor of MyApplication instead?

That would depend on what is happening in someCustomInit(). Your Application is not initialized yet.

Another possibility is to override attachBaseContext(), such as how ACRA is hooked in. There, you are passed a Context object that you can use, if your initialization requires a Context.

What is the difference between the Android constructor and onCreate()?

This graphic may help some.
http://developer.android.com/images/activity_lifecycle.png

In the Activity documentation they elaborate on what each function is meant for (i.e. onCreate(), onResume(), etc).
http://developer.android.com/reference/android/app/Activity.html

Android - Activity constructor

All of your initializations should be performed in the onCreate() method of your Activity:

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.cManager = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
this.mTextView = (TextView)this.findViewById(R.id.mProgressText);
}

Overriding the constructor of Activity involves quite a bit of heavy lifting, and is really not a walk in the park. Although you can [of course] have an empty constructor for an Activity, it really is quite superfluous in the context of the Android framework.

Related answers:

1. Why I cannot pass parameters to Android Activity Constructor.

2. Start an Activity with a parameter.

Why I cannot pass parameters to Android Activity Constructor

Refer to your code:

CalorieSelectorActivity csa = new CalorieSelectorActivity(userName);

Intent i = new Intent(thisContext, csa.getClass());

startActivity(i);

Even if you create an object of your activity, what you are "passing" in the Intent object is not the activity object but just the class of your activity. In startActivity() the Android framework will try to instantiate an object of your activity. And it calls the default constructor (without parameters) when it does that. It fails when your class does not have a constructor without parameters.

Of course, you have found the correct solution, pass the parameters as part of Intent object.

Member object instantiation in onCreate vs. during declaration

Functionally, nothing.

The first one will be created when the Activity object is created (new myClass() is called). The Android system does this at some point during creation.

The second one will be created when the system eventually calls onCreate().

The gotcha would be if you had an object that needs a Context in the constructor. You could do this for example:

public class myClass extends AppCompatActivity {
private objectType object = new objectType(this);

@Override
protected void onCreate(Bundle savedInstanceState) {
//do stuff with object

And the app will suddenly crash because you most likely will try to extract resources from the Context that don't exist yet. (Remember, the Activity isn't created at this point).

So, if your object does have to use Context, then you have to create it at or after onCreate is called.

calling oncreate within constructor

Well,i presume u r trying to call a constructor in an activity.If that is the case then some thing is wrong in the way u have designed ur project.For more details check out these links
Creating an object of Android Activity class and Android - Activity Constructor vs onCreate

Can you use a constructor in Child Activity to change Parent Activity String variable?

This can be done with abstract class because constructor don't construct an activity directly.

Try it like this

public abstract class ParentActivity extends Activity{
protected abstract String activityId();
.....
}

Then in child activity override activityId() with required value. And now you can compare activityId() in ParentActivity onCreate(...) method.

android activity class constructor working

Not sure why you would not want to use the intent params. That is what they are there for. If you need to pass the same parameters from different places in your application, you could consider using a static constructor that builds your intent request for you.

For example:

/**
* Sample activity for passing parameters through a static constructor
* @author Chase Colburn
*/
public class ParameterizedActivity extends Activity {

private static final String INTENT_KEY_PARAM_A = "ParamA";

private static final String INTENT_KEY_PARAM_B = "ParamB";

/**
* Static constructor for starting an activity with supplied parameters
* @param context
* @param paramA
* @param paramB
*/
public static void startActivity(Context context, String paramA, String paramB) {
// Build extras with passed in parameters
Bundle extras = new Bundle();
extras.putString(INTENT_KEY_PARAM_A, paramA);
extras.putString(INTENT_KEY_PARAM_B, paramB);

// Create and start intent for this activity
Intent intent = new Intent(context, ParameterizedActivity.class);
intent.putExtras(extras);
context.startActivity(intent);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Extract parameters
Bundle extras = getIntent().getExtras();
String paramA = extras.getString(INTENT_KEY_PARAM_A);
String paramB = extras.getString(INTENT_KEY_PARAM_B);

// Proceed as normal...
}
}

You can then launch your activity by calling:

ParameterizedActivity.startActivity(this, "First Parameter", "Second Parameter");



Related Topics



Leave a reply



Submit