Android can’t transfer value when using extras, bundle and intent

Working environment (bold blue for special attention)
1, System environment: Win7 Ultimate SP1, Android Studio 3.2
Oddly enough, today when a Bundle is used to pass values between activities, it is not possible to get the value passed in. Look at the pass-value code:

 Bundle bundle = new Bundle();
 bundle.putInt(EXAM_CENTER_TYPE, EXAM_CENTER_1);
 toActivity(ExamTypeActivity.class, bundle);

protected void toActivity(Class<?> clazz, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(this, clazz);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

Value code:

    protected Bundle getExtra() {
        Intent intent = getIntent();
        if (intent != null) {
            return getIntent().getExtras();
        } else {
            return null;
        }

    }

    protected String getExtraString(String key) {
        Bundle bundle = getExtra();
        if (bundle != null) {
            return bundle.getString(key);
        } else {
            return "";
        }
    }
    

    protected int getExtraInt(String key) {
      String extra = getExtraString(key);
      return NumberUtils.getInt(extra);
    }

GetExtraaint (EXAM_CENTER_TYPE) cannot get a value. The reason is that the data type passed to PUT must be the same as that of GET. If you put is a string, get is a string. The getExtraint function can be changed to the following function:

  protected int getExtraInt(String key) {
        Bundle bundle = getExtra();
        if (bundle != null) {
            return bundle.getInt(key);
        } else {
            return 0;
        }
    }

 
 

Read More: