Why is the Bundle object always null for onCreate ()?

I am trying to get into Android programming, and I took some examples from the book. This example suggests putting the following code:

public class ExemploCicloVida extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); Log.i(TAG, getClassName() + " onCreate() called on: " + icicle); TextView t = new TextView(this); t.setText("Exemplo de ciclo de vida de uma Activity.\nConsulte os logs no LogCat"); setContentView(t); } } 

I wonder why the Bundle object is always null in this case.

+7
source share
4 answers

The package will be empty if the state has not been previously saved.

This is mentioned in the Activity API documentation .

+13
source

In my case, the reason was that the specific action did not have a theme declared in the manifest file.

To fix this, open AndroidManifest.xml, click on "Application", select emergency activity in the "Application Sites" and add a subject to the "Subject" field of the attributes. In my case, it was

 @style/Theme.AppCompat.Light.DarkActionBar 

but you can copy the theme from one of your other activities.

PS: I know this is the answer to the old question, but I came across it while looking for a fix and did not find a working solution so that it could help others.

+8
source

Run this code and rotate the screen by pressing Ctrl + F11. The package will not be empty.

 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); if (savedInstanceState != null) { Toast.makeText(this, savedInstanceState.getString("s"), Toast.LENGTH_LONG).show(); } } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putString("s", "hello"); } 

onSaveInstanceState(Bundle) . Then an activity object is created and onCreated(Bundle) will be called with a non-zero Bundle savedInstanceState .

+2
source

I think you would like to read the parameters that are part of your activity. Use this function:

 protected String getStringExtra(Bundle savedInstanceState, String id) { String l; l = (savedInstanceState == null) ? null : (String) savedInstanceState .getSerializable(id); if (l == null) { Bundle extras = getIntent().getExtras(); l = extras != null ? extras.getString(id) : null; } return l; } 
0
source

All Articles