I am trying to setup the UI elements programatically.
I am able to setup the UI elements in onWindowFocusChanged method?
The question i want to ask is - shall i setup the UI elements in the onCreate method or on onWindowFocusChanged? The code -
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.baselayout);
}
And
@Override
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
res = getResources();
inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
setUpBackgroundImage();// setting up the background image
setUpTopMenu(); // Setting up the menu on top
setUpLogo(); // Setting up the Logo
}
}
Is the above approach correct?
onCreate(Bundle savedInstanceState) Function in Android: When an Activity first call or launched then onCreate(Bundle savedInstanceState) method is responsible to create the activity.
By calling super. onCreate(savedInstanceState); , you tell the Dalvik VM to run your code in addition to the existing code in the onCreate() of the parent class. If you leave out this line, then only your code is run. The existing code is ignored completely.
OnCreate will only be called one time for each lifetime of the Activity. However, there are a number of situations that can cause your activity to be killed and brought back to life. Thus, onCreate will be called again.
onCreate(savedInstanceState); calls the method in the superclass and saved InstanceState of the activity if any thing damage the activity so its saved in instanceState so when reload the activity it will be the same before. Show activity on this post.
Take note that some new devices are capable of showing multiple windows, onWindowFocusChanged()
is not ideal place to initialize your layout. Use onCreate()
to inflate layout and set up View variables.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With