Bundle is null after setting it in Intent

前端 未结 2 1351
礼貌的吻别
礼貌的吻别 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:36

    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);
    
    0 讨论(0)
  • 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");
    
        }
    
    0 讨论(0)
提交回复
热议问题