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

前端 未结 3 1106
轻奢々
轻奢々 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:22

    In order to pass the parameters you create new intent and put a parameter map:

    Intent myIntent = new Intent(this, NewActivityClassName.class);
    myIntent.putExtra("firstKeyName","FirstKeyValue");
    myIntent.putExtra("secondKeyName","SecondKeyValue");
    startActivity(myIntent);
    

    In order to get the parameters values inside the started activity, you must call the get[type]Extra() on the same intent:

    // getIntent() is a method from the started activity
    Intent myIntent = getIntent(); // gets the previously created intent
    String firstKeyName = myIntent.getStringExtra("firstKeyName"); // will return "FirstKeyValue"
    String secondKeyName= myIntent.getStringExtra("secondKeyName"); // will return "SecondKeyValue"
    

    If your parameters are ints you would use getIntExtra() instead etc. Now you can use your parameters like you normally would.

提交回复
热议问题