Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OnWindowFocusChanged and onCreate - Android

Tags:

android

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?

like image 976
Anukool Avatar asked Jan 29 '13 05:01

Anukool


People also ask

What does the onCreate do in Android?

onCreate(Bundle savedInstanceState) Function in Android: When an Activity first call or launched then onCreate(Bundle savedInstanceState) method is responsible to create the activity.

What is super onCreate in Android?

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.

Why is onCreate called twice?

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.

What does onCreate method do?

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.


1 Answers

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.

like image 67
S.D. Avatar answered Nov 15 '22 14:11

S.D.