0

I am trying to reload my activity and pass a bundle, but I'm getting an empty (null) bundle.

Reload activity:

Intent intent = new Intent(MyActivity.this, MyActivity.class);
Bundle bundle = new Bundle();
bundle.putInt("key", 1);
intent.putExtras(bundle);
MyActivity.this.finish();
startActivity(intent);

onCreate activity and I should get the bundle:

@Override
public void onCreate(Bundle savedInstance)
{
   if (savedInstance != null)
   {
   }
   else
   {
      Log.i("d", "IS NULL !");
   }
}

I'm getting null.

mmBs
  • 8,421
  • 6
  • 38
  • 46
Zbarcea Christian
  • 9,367
  • 22
  • 84
  • 137
  • this might help http://stackoverflow.com/questions/768969/passing-a-bundle-on-startactivity – mihail Sep 17 '13 at 08:49

2 Answers2

5

In OnCreate() you should do like this :

if(getIntent().getExtras() != null) {
    Bundle extras = getIntent().getExtras();
    Log.i("Value", extras.getString("key"));
}

Instead of this

if (savedInstance != null){
}
Harish Godara
  • 2,388
  • 1
  • 14
  • 28
2

First start the activity and then call finish() as follows:

Intent intent = new Intent(MyActivity.this, MyActivity.class);
Bundle bundle = new Bundle();
bundle.putInt("key", 1);
intent.putExtras(bundle);
startActivity(intent);
MyActivity.this.finish();

Then receive the bundle extras like this:

Bundle bundle = getIntent().getExtras();

Finally, you can put conditions to check if it's correct like:

if(bundle != null)
{
}
else
{
   Log.i("d", "IS NULL !");
}
sjain
  • 23,126
  • 28
  • 107
  • 185