How to start an Intent by passing some parameters to it?

前端 未结 3 1102
轻奢々
轻奢々 2020-11-28 20:23

I would like to pass some variables in the constructor of my ListActivity

I start activity via this code:

startActivity(new Intent (this, viewConta         


        
3条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-28 21:14

    putExtra() : This method sends the data to another activity and in parameter, we have to pass key-value pair.

    Syntax: intent.putExtra("key", value);

    Eg: intent.putExtra("full_name", "Vishnu Sivan");

    Intent intent=getIntent() : It gets the Intent from the previous activity.

    fullname = intent.getStringExtra(“full_name”) : This line gets the string form previous activity and in parameter, we have to pass the key which we have mentioned in previous activity.

    Sample Code:

    Intent intent = new Intent(getApplicationContext(), MainActivity.class);
    intent.putExtra("firstName", "Vishnu");
    intent.putExtra("lastName", "Sivan");
    startActivity(intent);
    

提交回复
热议问题