How do I get extra data from intent on Android?

后端 未结 16 2325
清歌不尽
清歌不尽 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:50

    Pass the intent with value on First Activity:

    Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
    intent.putExtra("uid", uid.toString());
    intent.putExtra("pwd", pwd.toString());
    startActivity(intent);
    

    Receive intent on second Activity;-

    Intent intent = getIntent();
    String user = intent.getStringExtra("uid");
    String pass = intent.getStringExtra("pwd");
    

    We use generally two method in intent to send the value and to get the value. For sending the value we will use intent.putExtra("key", Value); and during receive intent on another activity we will use intent.getStringExtra("key"); to get the intent data as String or use some other available method to get other types of data (Integer, Boolean, etc.). The key may be any keyword to identify the value means that what value you are sharing. Hope it will work for you.

提交回复
热议问题