Bundle is null after setting it in Intent

前端 未结 2 1365
礼貌的吻别
礼貌的吻别 2020-12-20 16:26

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

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-20 16:51

    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");
    
        }
    

提交回复
热议问题