Passing arguments to intent android

ⅰ亾dé卋堺 提交于 2021-01-28 06:09:01

问题


I have the first intent, it starts the second intent. In the second intent, I get the values, and pass the value to the first content and close the second content. How can I do it?


回答1:


You can directly pass parameters to the intent when you create it. If you need to pass objects you need to implement Parcelable interface on the object you pass:

Intent i = new Intent(MyActivity.this, SecondActivity.class);
MyData j = new MyData();
i.putExtra("MyParameter", "Something");
i.putExtra("MyData", j); //only works if MyData implements Parcelable
startActivity(i);

In the second activity you can read your data:

Intent i = getIntent();
Bundle extras = i.getExtras();
if(extras.containsKey("MyParameter")) {
    String something = i.getStringExtra("MyParameter");
}
if(extras.containsKey("MyData")) {
    MyData otherthing = i.getParcelableExtra("MyData");
}

Hope this helps




回答2:


try this

Passing the parameter to using intent i am passing the message like this

     Intent intent= new Intent(mContext,SuccessActivity.class);

     intent.putExtra("message",mContext.getString(R.string.success_sign_msg));

Get value using intent

     Intent intent=getIntent();
    success_msg_txt.setText(intent.getStringExtra("message"));

try this it helps you



来源:https://stackoverflow.com/questions/51181747/passing-arguments-to-intent-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!