Reputation: 41759
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?
Upvotes: 30
Views: 41751
Reputation: 21
I've inherited an old app which uses the old showDialog()
-API to show (amongst other things) an progress dialog for some initialisation tasks. What happens behind the scenes is that Android will track these dialogs in a mManagedDialogs
variable the gets stored on the base Activity
class.
While shuffling some things about, I found out that calling showDialog()
in onCreate()
is a bad idea, because if the Activity is in fact being recreated (as opposed to being started completely afresh), mManagedDialogs
is going to be clobbered by Android restoring the previous state in onRestoreInstanceState()
, which means I can't programmatically close the dialog I opened during onCreate()
any more, because its data has been overwritten by the old data which has been restored by the framework.
So under those circumstances, it seems that onPostCreate()
is the earliest point that at which I can start calling showDialog()
(after super.onRestoreInstanceState()
would work, too, but onRestoreInstanceState()
doesn't get called on every activity launch).
Upvotes: 0
Reputation: 29
You can check the execution sequence in ActivityThread#performLaunchActivity
. Then you will find that onPostCreate()
is the final lifecycle method thad has been executed after the onCreate(),onStart(),OnRestoreInstanceState()
of an activity. And after that,onResume
will be executed.
Upvotes: 1
Reputation: 733
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.
Upvotes: 61
Reputation: 1135
it just called after onCreate, Since my program isn`t very complex, it works well. It is useful to do something in common in the base class after the child class has inflate its layout
Upvotes: -1
Reputation: 3582
This is an extension to the second answer:
Imagine you are trying to implement BaseActivity which doesn't set any layout in OnCreate method.
public abstract class BaseActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
Then imagine you have some other Activity (extends the BaseActivity) which sets some layout :
public class MainActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/my_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
So the the very first time when you can use your button in BaseActivity is onPostCreate method:
public abstract class BaseActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
Button btn = (Button) findViewById(R.id.myBtn); //this is the when you can initialise your button
}
}
Using BaseActivity is a common practice in making good apps!
Upvotes: 19
Reputation: 169
According to the event name 'Post' meaning, i always use it for checking Elements size might be changed during onCreated, especially while Elements or layouts should be changed after screen rotated.
Upvotes: 0
Reputation: 855
Since none of the answers made a point on onRestoreInstanceState(Bundle), I want to make a point here. This method will be called, If an app is forcefully removed from the memory and then started by the user again. So that we can use this method to retain activity state, Incase if the user previously removed the app forcefully from memory.
Upvotes: 0
Reputation: 4717
There might be situations you will be needing to use it. specially with the newer APIs.
A scenario that it might be useful is during rotation change, or return state to an activity that has a progress bar inside the action bar.
you will need to set it false inside it onPostCreate()
@Override
protected void onPostCreate(Bundle savedInstanceState)
{
super.onPostCreate(savedInstanceState);
setProgressBarIndeterminateVisibility(false);
}
Upvotes: 1
Reputation: 5758
I use onPostCreate()
when i need to change the view programmatically. Because call findViewById()
not work when i use in onCreate
.
Upvotes: 1
Reputation: 9716
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...
Upvotes: 20
Reputation: 4335
As the documentation states, onPostCreate is mostly intended for framework use. The question is: What do you intend to do in onPostCreate() that you can't do in onCreate() or onResume() (i.e. what exactly does "tweaking some elements" mean)?
I am not using it, as I see no reason to do so - all I need to do can be done in onCreate or onResume. However Google itself uses it in it's TabActivity for instance.
Upvotes: 17