3

In onCreate(Bundle bdl){}, we must call its super constructor by super.onCreate(bdl).

For newly created activities, we got a null Bundle in onCreate(Bundle bdl){}. So when we call super.onCreate(bdl), it is the same as calling super.onCreate(null).

For reconstructed activities (like after rotate), we got a non-null Bundle. But I notice even if we call super.onCreate(null), instead of super.onCreate(bdl), it seems to be just the same. The layout restoration works are done in super.onRestoreInstanceState(bdl).

So, is it really true that calling super.onCreate(null) is the same as calling super.onCreate(bdl) in all the cases?

Thanks.

midnite
  • 5,157
  • 7
  • 38
  • 52
  • I think it is different...When your application pause for sometime, How can you restore it..? if you pass null – Pragnani Feb 27 '13 at 15:39

2 Answers2

10

According to the Android Source code, the Activity.onCreate() method forwards the saveInstanceState bundle to the activity's fragments. To be more specific, it fetches a parcelable with the "android:fragments" key and forwards this parcelable to the fragments using the FragmentManager.restoreAllStates() method, which itself restore the state on all fragments.

The Activity.onRestoreInstanceState() method forwards the bundle to the activity's window. Again it fetches the "android:viewHierarchyState" bundle from the saved instance and forwards it the the window using the Window.restoreHierarchyState() method.

So to answer your question, if your activity doesn't use Fragments, then indeed calling super.onCreate(null) won't change anything. But as best practice, I'll advise you to always forward the exact savedInstance bundle (unless you know what you're doing).

Edit : here are the sample source codes I talked about, taken from AOSP v17 :

Activity.java

protected void onCreate(Bundle savedInstanceState) {

    // [... some content ellipsed for readability purposes]

    if (savedInstanceState != null) {
        Parcelable p = savedInstanceState.getParcelable(FRAGMENTS_TAG);
        mFragments.restoreAllState(p, mLastNonConfigurationInstances != null
                ? mLastNonConfigurationInstances.fragments : null);
    }
    mFragments.dispatchCreate();
    getApplication().dispatchActivityCreated(this, savedInstanceState);
    mCalled = true;
}


// [...]

protected void onRestoreInstanceState(Bundle savedInstanceState) {
    if (mWindow != null) {
        Bundle windowState = savedInstanceState.getBundle(WINDOW_HIERARCHY_TAG);
        if (windowState != null) {
            mWindow.restoreHierarchyState(windowState);
        }
    }
}
XGouchet
  • 10,002
  • 10
  • 48
  • 83
  • Nice! Thanks for answering! I will try about the Fragments soon, Could you please quote whereabouts in the Android docs talking about this? So I can read more in depth. – midnite Feb 27 '13 at 16:00
  • Well it's not in the documentation but in the source code. I'll edit my answer accordingly. – XGouchet Feb 27 '13 at 16:05
  • Thanks again! If you could give us a brief example about what are the differences in Fragments would be very very very appreciated!!! – midnite Feb 27 '13 at 16:09
  • Sorry i need some times to digest and try about it. I surely will come back and tick your answer after i understand it. Lots of thanks! – midnite Feb 27 '13 at 18:35
  • 1
    Simply say, if we use `super.onCreate(null);`, we cannot restore our `Fragment`s. I have tested it in my code. I have another problem about `Fragment` and `saved state`. Could you please help? http://stackoverflow.com/questions/15858854/where-fragment-save-its-state-when-fragmenttransaction-replace Thanks a lot!! – midnite Apr 07 '13 at 09:47
  • @XGouchet hi i'm got into a serious problem. think of a situation where i have hided and added fragments (2 or 3) with add to Back stack. Now if i'm at fragment 3 all the previous fragments are hidden and in back stack. i press home key. kill the process from ddmms. When i came back to application. The previous 2 hidden fragments are showing upon in the background of 3. Also pressing back key doesn't remembered the transition animation. Could you please help! – Muhammad Babar Apr 02 '14 at 12:33
  • I know this is normal behaviour. Everything is recreated but how can i handle fragments so they won't get visible. – Muhammad Babar Apr 02 '14 at 12:35
  • @XGouchet What do you thin about this question: http://stackoverflow.com/questions/26142255/retrieve-an-activity-after-time-out-warning-notification – Hamid Oct 01 '14 at 17:41
0

The easiest way for you to find this out is by using Log() utility.

Although, bare in mind that you can put stuff into the bundle with

Bundle bdl = new Bundle(1);
bdl.putString("file_absolute_path", f.getAbsolutePath());
cf.setArguments(bdl);

And retrieve them with getArguments().

So in short - it depends on whether you're using bundle arguments in your app. If no, then it's probably the same.

Tool
  • 12,126
  • 15
  • 70
  • 120