Using intents for passing data

前端 未结 5 1014
庸人自扰
庸人自扰 2020-12-12 07:58

How do you send data from one activity to another using intents.

(Note this is a two-part question, the sending and the receiving).

I\'m creating a form, a

相关标签:
5条回答
  • 2020-12-12 08:06

    To pass the data using intent from the current activity use the following code

                 Intent in = new Intent(YourClassContext, nextActivity.class);
                 in.putExtra("name","value");
                 startActivity(in);
    

    To get the intent data passed in the next activity use the following code

         Intent i = nextActivity.this.getIntent();
         String intentDatapassed = i.getStringExtra("name", defaultValue);
    
    0 讨论(0)
  • 2020-12-12 08:08

    You can pass data easily using Bundle.

    Bundle b=new Bundle();
    b.putString("key", value);
    
    Intent intent=new Intent(TopicListController.this,UnitConverter.class);
    intent.putExtras(b);
    
    startActivity(intent);
    

    You can recieve data in your other activity like this as follow:

    Bundle b=this.getIntent().getExtras();
    String s=b.getString("select");
    
    0 讨论(0)
  • 2020-12-12 08:15
    Intent i = new Intent(yourActivity.this, nextActvity.class);
    
    i.putExtra("dataName", data);
    
    //recieving
    
    Intent i = nextActivity.this.getIntent();
    
    
     string s =  i.getStringExtra("dataName", defaultValue);
    
    0 讨论(0)
  • 2020-12-12 08:17

    Sending (Activity 1):

    Intent intent = new Intent(MyCurentActivity.this, SecondActivity.class);
    intent.putExtra("key", "enter value here");
    startActivity(intent); 
    

    Receiving (Activity 2):

    String text = getIntent().getStringExtra("key");
    
    0 讨论(0)
  • 2020-12-12 08:25

    To send from the Activity

    Intent myIntent = new Intent(First.this, Second.class);
        myIntent.putExtra("name","My name is");
        startActivity(myIntent);
    

    To retrieve in the secod

    Bundle bundle = getIntent().getExtras();
               String name=bundle.getString("name");
    
    0 讨论(0)
提交回复
热议问题