Reputation: 33956
I have many activities in which I need to execute some code on create, this code has to do with display preferences such as different themes, hiding the status bar etc.
This is an example of one of the activites:
public class MainActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
}
}
Base activity in turn has this code:
public class BaseActivity extends Activity{
// getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
// System.out.println("BaseActivity");
}
I'm trying to run some code on create but I cannot manage to do it. How can I run the code from above as soon as possible using BaseActivity?
Upvotes: 1
Views: 1954
Reputation: 18381
call super.onCreate(Bundle bundle) in the childs onCreate method to call the onCreate of the parent.
This is a common practice to abstract code from an Activity for reuse in other activities.
e.g.:
public class MainActivity extends BaseActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v(TAG,"MainActivity");
}
}
public class BaseActivity extends Activity{
protected static final String TAG = "TAG";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v(TAG,"BaseActivity");
}
}
I personnaly do this a lot but I often prefer the following for having better maintainable code:
composition over inheritance
Upvotes: 6