简体   繁体   中英

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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM