So I managed to solve a problem of retrieving information from the bundle, but I had to do something which I thought was weird. The method I tried first was this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
list = savedInstanceState.getInt("listmenu");
}
And this is how I am putting information into the bundle and calling the activity:
Intent myIntent = new Intent(MainActivity.this, NewActivity.class);
myIntent.putExtra("listmenu", R.menu.listmenu);
MainActivity.this.startActivity(myIntent);
But that didn’t work. It would just crash on line 3, the list where I try to getInt (I couldn’t find out why exactly, but it didn’t work).
Then after some googling I tried it this way:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
list = extras.getInt("listmenu");
}
It seems to me like getIntent().getExtras() would just return savedInstanceState. But if this were the case then my first method would have worked. So I must misunderstand either what savedInstanceState is, or what getIntent() does, so could someone explain what the difference between these two things is?
A bundle is passed to onCreate() if the activity is being destroyed and then re-created, ie on a configuration change. If you would like to put data to save when your activity is re-created, you would want to override onSaveInstanceState. This bundle will be passed to both onCreate and onRestoreInstanceState.
The second block of code you included is correct for reading bundles passed to newly starting activities. Does this clarify things? The best thing to do is read the description of each in the Activity doc.