This is a basic question but I need some help with it.
I have two activities : actA, actB. While in actA I want to start actB and give it a String, than I want to end actB and return another String to actA (I don't want to go to onCreate() of actA, I would much rather return this value to some method in actA so it can use the String from actB.
Help is appreciated
From A.java:
Intent myintentB=new Intent(A.this, B.class).putExtra("<StringName>", "Value");
startActivityForResult(myintentB, 3);
from B.java:
Intent myintentA=new Intent(B.this, A.class).putExtra("<StringName>", "Value");
finish();
setResult(3, myintentA);
In A.java
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
String result_string=data.getStringExtra("<StringName>");
}
In Activity A :
Intent intent = new Intent();
intent.setClass (getApplicationContext(), ActB.class) ;
intent.putExtra ("data1","NEW STRING") ;
context.startActivityForResult(intent) ;
In Activity B (onCreate Method) :
Intent intent = getIntent() ;
if (intent.hasExtra("data1") )
String dataSent = intent.getStringExtra("data1") ;
While sending data back :
Intent intent = new Intent() ;
intent.putExtra ("Return" , "RETURN STRING") ;
setResult(RESULT_OK, intent) ;
finish() ;
In Activity A : (onActivityResult) [ You need to override ]
if (data.hasExtra("Return"))
String data1 = data.getStringExtra("Return");
the answer to question linked below also describes the same issue you are looking for
As for your first problem, you can give extra values to a new Intent by using the method intentname.putExtra("extravalue", value);
before actually creating the Intent. You can then read that value out in the the newly created Activity. I know I'm pretty terrible at explaining, but I hope you get the idea from this piece of code I took from an app I made.
ListLinks.java:
// Pass the value of the item URL to the linkviewer when a link is clicked
Intent openLink = new Intent(this, LinkView.class);
openLink.putExtra("url" , item.URL);
startActivity(openLink);
LinkView.java
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.linkview);
Bundle bun = getIntent().getExtras();
String url = bun.getString("url");
I'm not quite sure about how to resolve your second problem, so sadly I cannot help you with this one but I'm sure someone else might.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.