user1290384
user1290384

Reputation:

Hello World Android help in understanding Bundle

A basic doubt. I m working with the following code.

package com.example.helloandroid;

import android.app.Activity;
import android.os.Bundle;

public class HelloAndroidActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}
}

What will the Bundle savedInstanceState contain?
Since oncreate is an overridden method, I feel that "savedInstanceState" will not get any information from the base class.
If that is the case, super.onCreate(savedInstanceState) cannot be executed.
Please help me in where my understanding is going wrong.Thanks.

Upvotes: 0

Views: 103

Answers (1)

dldnh
dldnh

Reputation: 8951

it will have whatever you put there in your @Override of onSaveInstanceState.

onSaveInstanceState will be called whenever the system needs to save its state for later re-creation. change of orientation is one of these cases. if you switch between portrait and landscape, onSaveInstanceState will be called, and you'll fill in the bundle that's passed in. then onCreate will be called with that same bundle.

so then whenever onCreate is called with a savedInstanceState, you should initialize your activity from this instead of from scratch.

EDIT: explanation copied from comments:

in your onSaveInstanceState you will call the Bundle's putXxxx methods -- whatever values represent the current state of your activity, you'll "put" that into the Bundle. then you'll get back that same bundle in a future onCreate so you can re-create your activity from where you left off.

if you're not implementing onSaveInstanceState, the savedInstanceState parameter will be null in onCreate and you can safely ignore it.

Upvotes: 1

Related Questions