Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do you use onPostCreate() method?

Tags:

android

According to the documentation,

Called when activity start-up is complete (after onStart() and onRestoreInstanceState(Bundle) have been called). Applications will generally not implement this method; it is intended for system classes to do final initialization after application code has run.

Derived classes must call through to the super class's implementation of this method. If they do not, an exception will be thrown.

It is not advised to use this method. However, I use it to tweak some elements after onCreate. I see that some people use it to do something between onResume() and they are advised not to do this as they cannot rely on this method (due to its bad documentation).

So, can I use the tweaking here (it does not rely on onResume at all)?
Do you ever use this method and when/why?

like image 812
sandalone Avatar asked Oct 01 '11 16:10

sandalone


People also ask

What is onPostCreate?

using onPostCreate as a callback method from subclass' onCreate will notify that all creation is finished. Example: If you have activities sharing the same layout, then you can use the onPostCreate to add onClickListeners etc. If you are overriding onPostCreate, it is best to make the call to super.

Why do we need to call setContentView () in onCreate () of activity class?

As onCreate() of an Activity is called only once, this is the point where most initialization should go: calling setContentView(int) to inflate the activity's UI, using findViewById to programmatically interact with widgets in the UI, calling managedQuery(android.


2 Answers

onPostCreate can be useful if you are making your own "super-class" extension of Activity with functionality that all your activities shall share.

using onPostCreate as a callback method from subclass' onCreate will notify that all creation is finished. Example: If you have activities sharing the same layout, then you can use the onPostCreate to add onClickListeners etc

If you are overriding onPostCreate, it is best to make the call to super.onPostCreate at the end of your implementation.

like image 199
user1419056 Avatar answered Sep 28 '22 07:09

user1419056


Google uses onPostCreate() in their example project for Navigation Drawer. ActionBarDrawerToggle needs to sync after orientation change lets say :)

@Override protected void onPostCreate(Bundle savedInstanceState) {     super.onPostCreate(savedInstanceState);      // Sync the toggle state after onRestoreInstanceState has occurred.     mDrawerToggle.syncState(); } 

So I think onPostCreate() should be used only in some specific situations...

like image 45
lomza Avatar answered Sep 28 '22 08:09

lomza