Transfer data from one Activity to Another Activity Using Intents

后端 未结 8 1593
花落未央
花落未央 2020-11-29 06:36

I would like to be able to transfer data from one activity to another activity. How can this be done?

8条回答
  •  旧巷少年郎
    2020-11-29 07:08

    Your Purpose

    Suppose You want to Go From Activity A to Activity B.

    So We Use an Intent to switch activity

    the typical code Looks Like this -

    In Activity A [A.class]

    //// Create a New Intent object
    Intent i = new Intent(getApplicationContext(), B.class);
    
    /// add what you want to pass from activity A to Activity B
    i.putExtra("key", "value");
    
    /// start the intent
    startActivity(i);
    

    In Activity B [B.class]

    And to Get the Data From the Child Activity

    Intent i = getIntent();
    
    if (i!=null) {
       String stringData= i.getStringExtra("key");
    }
    

提交回复
热议问题