开发者

Android: How to pass data of one activity with the button click in another activity [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question开发者_运维知识库 so that it can be reopened, visit the help center. Closed 10 years ago.

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.


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();


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.


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


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


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();


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜