How do I get extra data from intent on Android?

后端 未结 16 2263
清歌不尽
清歌不尽 2020-11-21 11:01

How can I send data from one activity (intent) to another?

I use this code to send data:

Intent i=new Intent(context,SendMessage.class);
i.putExtra(\         


        
16条回答
  •  隐瞒了意图╮
    2020-11-21 11:54

    Put data by intent:

    Intent intent = new Intent(mContext, HomeWorkReportActivity.class);
    intent.putExtra("subjectName", "Maths");
    intent.putExtra("instituteId", 22);
    mContext.startActivity(intent);
    

    Get data by intent:

    String subName = getIntent().getStringExtra("subjectName");
    int insId = getIntent().getIntExtra("instituteId", 0);
    

    If we use an integer value for the intent, we must set the second parameter to 0 in getIntent().getIntExtra("instituteId", 0). Otherwise, we do not use 0, and Android gives me an error.

提交回复
热议问题