Android: How to pass the data to sub-activities?

后端 未结 3 1399
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-09 05:36

The main activity includes some variables with set values. I created a sub-activity with the form which has to be filled with the data from main activity so I guess the data

相关标签:
3条回答
  • 2020-12-09 06:11

    You can use this method in your main activity

    Intent i = new Intent(this, YourMainClass.class);
    i.putExtra("key", value);
    

    end then in the sub activity get the value with this method, usually in the onCreate event

    int value = getIntent().getExtras().getInt("key");
    

    I hope this hepls.

    0 讨论(0)
  • 2020-12-09 06:20

    Will this work in the main activity?

    Intent i = new Intent(this, YourMainClass.class);
    i.putExtra("key", value);
    

    Followed by:

    String value = getIntent().getExtras().getString("key");
    

    And can you add multiple "Extras" like or something like this?

    i.putExtra("key", value1); 
    i.putExtra("key2", value2);
    i.putExtra("key3", value3);
    

    Thanks...

    0 讨论(0)
  • 2020-12-09 06:23

    Try this it will work:

    activity1.class:

    Intent i = new Intent(activity1.this,activity2.class);
    
    Bundle b = new Bundle();
    b.putString("name", "your value need to pass here");
    
    i.putExtras(b);
    startActivity(i);
    

    activity2.class:

    Bundle b = this.getIntent().getExtras();
    
    String name = b.getString("name");
    
    ((TextView)findViewById(R.id.textView1)).setText(name);
    
    0 讨论(0)
提交回复
热议问题