Reputation: 4589
Let me show you 2 examples of my code.
example 1:
@Override
protected void onCreate(Bundle savedInstanceState) {
if (savedInstanceState != null){
mCurrentIndex = savedInstanceState.getInt(KEY_INDEX, mCurrentIndex);
};
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
}
example 2:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
if (savedInstanceState != null){
mCurrentIndex = savedInstanceState.getInt(KEY_INDEX, mCurrentIndex);
};
}
I'm wondering how come it doesn't matter where I put
if (savedInstanceState != null){
mCurrentIndex = savedInstanceState.getInt(KEY_INDEX, mCurrentIndex);
};
in my code. My hypothesis was that setContentView
displays the current layout, which is different if mCrrentIndex
is changed. So it would matter if I FIRST set the content view and THAN check what int does the mCurrentIndex
have. It turns out thats not the case and I don't know why. Could anyone explain?
Upvotes: 1
Views: 230
Reputation: 31846
In the onCreate
method, no views have yet been created, they are inflated at a later stage of the Activity lifecycle. So when they are loaded, the new value of mCurrentIndex
will be used, since even if you set it after you call setContentView
, it is still before any views have been created.
Upvotes: 1