Android: How to pass data of one activity with the

2019-09-22 08:13发布

I have two classes like firstactivity.java and secondactivity.java. In firstactivity I have a button(submit) when I click the button(submit) I want to pass the data of firstactivity.java to server. How can I do this?

Thanks in advance.

6条回答
Anthone
2楼-- · 2019-09-22 08:37

In FirstActivity.java file onclick button you should used below code.

Intent i1 = new Intent(firstactivity.this, secondactivity.class);
i1.putExtra("type", "edit");
startActivity(i1);

In secondActivity.java file oncreate .. used below code.

Bundle extras = getIntent().getExtras();
Strinjg Value = extras.getSerializable("type").toString();
查看更多
▲ chillily
3楼-- · 2019-09-22 08:42

You can pass data from one activity to another activity by using this:

Intent i=new Intent(firstactivty.class,secondactivity.class);
i.putExtra("String","abc");
startActivity(i);

And you can get this data to secondActivity by using this:

Bundle extras;
extras =getIntent().getExtras();
string value=extras.getString("String");

But remember one thing: keyword would be same when you pass the data as well as getdata, for example:

i.putExtra("String","abc"); //pass value
extras.getString("String"); //get value
查看更多
SAY GOODBYE
4楼-- · 2019-09-22 08:42

You can use the putExtra(String name, Bundle value) method of Intent class to send data to second activity. Get this data in second activity from a Bundle object's getExtra() method.

查看更多
smile是对你的礼貌
5楼-- · 2019-09-22 08:43

For passing data between activities you could use set extra methods of intent which you use for starting activity link

Also you can use Bundle to pass data between parts of you program

Bundle bundle = new Bundle();
bundle.putInt("int-value",10); // put data to bundle
int value = bundle.getInt("int-value",0); // gets value from bundle, or 0 (second parameter)

You can return data from second activity to first using method setResult

// Somewhere in your activity
Intent result = new Intent();
result.putExtra("result-value",10);
setResult(RESULT_OK,result);
finish();
查看更多
乱世女痞
6楼-- · 2019-09-22 08:47

You can add things to a bundle and add the bundle to the intent.

Then read the bundle in the new activity and get what you need from it. There should be hundreds of posts on Google and SO about this.

查看更多
再贱就再见
7楼-- · 2019-09-22 08:57

First send the second activity data to your first activity using intents,then get that data in First Activity using getIntent() method or you can store those data in static fields then you can get the data wherever you want

查看更多
登录 后发表回答