Bundle is null after setting it in Intent

22,872

Solution 1

I am not sure what "Info" is for, but I suggest making the most basic passing of data from one activity to another first before involving other data objects.

Activity1

    Intent intent = new Intent(Activity1.this, Activity2.class);
    intent.putExtra("asdf", true);
    info.write(intent);
    startActivity(intent);

Activity2

    Bundle bundle = getIntent.getExtras();
    if (bundle!=null) {
        if(bundle.containsKey("asdf") {
            boolean asdf = bundle.getBooleanExtra("asdf");
            Log.i("Activity2 Log", "asdf:"+String.valueOf(asdf));
        }
    } else {
        Log.i("Activity2 Log", "asdf is null");

    }

Solution 2

Activity 1

Intent intent = new Intent(getApplicationContext(), MapViewActivity.class);

        Bundle b = new Bundle();
         b.putBoolean("asdf", true);
         b.putInt(AppConstants.ID_KEY, id);
         intent.putExtras(b);

         startActivity(intent);

Activity 2

Bundle extras = getIntent().getExtras();

 boolean bool = extras.getBoolean("asdf");
 int m_int = extras.getInt(AppConstants.ID_KEY,-1);
Share:
22,872
Xeon
Author by

Xeon

I'm Java environment developer (mostly). I also know Javascript full-stack. I have also experience in PHP environment and of course front end technologies. Currently I'm a business owner at Teligent.

Updated on July 26, 2022

Comments

  • Xeon
    Xeon almost 2 years

    I know there are questions like: android-intent-bundle-always-null and intent-bundle-returns-null-every-time but there is no correct answer.

    In my Activity 1:

    public void goToMapView(Info info) {
        Intent intent = new Intent(getApplicationContext(), MapViewActivity.class);
        //intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        intent.putExtra("asdf", true);
        info.write(intent);
        startActivity(intent);
    }
    

    In Info:

    public void write(Intent intent) {
        Bundle b = new Bundle();
        b.putInt(AppConstants.ID_KEY, id);
        ... //many other attributes
        intent.putExtra(AppConstants.BUNDLE_NAME, b);
    }
    public static Info read(Bundle bundle) {
        Info info = new Info();
        info.setId(bundle.getInt(AppConstants.ID_KEY));
        ... //many other attributes
        return info;
    }
    

    In MapViewActivity (Activity 2):

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map_view);
    
        Bundle extras = getIntent().getBundleExtra(AppConstants.BUNDLE_NAME);
        info = Info.read(extras);
        ...
    }
    

    Problem is that extras bundle is always null. I have debugged it and Intent (intent = getIntent()) has all fields set to null except one indicating what class this is (MapViewActivity).

    I've also tried putting the bundle via intent.putExtras(b) with the same effect. The intent.putExtra("asdf", true) is for debugging reasons only - I cannot get this data too (because getIntent() has almost all fields set to null).

    EDIT

    The answers below are correct and working. This was my fault. I didn't correctly passed my bundle to new intent.

  • Xeon
    Xeon about 12 years
    Info class is for aggregating information that I need. It has static 'read' and instance 'write' methods for better readability. The most basic passing of data is represented by putting "asdf" and this don't work. And did you mean getBoolean instead of getBooleanExtra? There is no such method. I just check your example - I get asdf is null. But thanks anyway.
  • Xeon
    Xeon about 12 years
    I've tried it as it states in question at the bottom: "I've also tried putting the bundle via intent.putExtras(b) with the same effect". There is nothing wrong with Info class.
  • Ravi1187342
    Ravi1187342 about 12 years
    above shold work fine. anyway try to put values directly in intent like intent.putExtra("asdf",false); and intent.putExtra(AppConstants.ID_KEY,id);
  • Scott Jodoin
    Scott Jodoin almost 2 years
    First line needs to be Bundle bundle = getIntent().getExtras(); The two if statements can be combined to form can be if (bundle != null && bundle.containsKey("asdf")) { thanks to short circuiting.