Anukool
Anukool

Reputation: 5391

OnWindowFocusChanged and onCreate - 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?

Upvotes: 8

Views: 14797

Answers (2)

She Smile GM
She Smile GM

Reputation: 1322

onCreate() This is the place where you setup your UI

onWindowFocusChanged() This is called when all your layouts or UI have been successfully loaded or created properly.

Upvotes: 4

s.d
s.d

Reputation: 29436

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.

Upvotes: 6

Related Questions