Use an intent to send data to my activity

前端 未结 2 1350
野性不改
野性不改 2020-12-11 01:35

I have a server running that notifies the user with a statusbar notification that opens my main activity, how can I pass data to my activity trough that intent?

相关标签:
2条回答
  • 2020-12-11 02:01

    Use Intent.putExtra(..):

    intent.putExtra("keyName", "somevalue");
    

    This method is overloaded and takes various types as second argument: int, byte, String, various arrays..

    To get the data out use appropriate getXYZExtra(). For String this is:

    getStringExtra(String keyName)
    
    0 讨论(0)
  • 2020-12-11 02:12

    MainActivity

    Intent intent = new Intent(MainActivity.this,SecondActivity.class);
    intent.putExtra("extra_text", string); 
    startActivity(intent);
    

    SecondActivity

     String text = getIntent().getStringExtra("extra_text");
    
    0 讨论(0)
提交回复
热议问题