I searched on the forums but couldn't find the right answer for me.
I've included the relevant parts below
ACTIVITY ONE
implicitActivationButton.setOnClickListener(new OnClickListener() {
// Call startImplicitActivation() when pressed
#Override
public void onClick(View v) {
Intent myIntent = new Intent(ActivityLoaderActivity.this,
ExplicitlyLoadedActivity.class);
startActivityForResult(myIntent, GET_TEXT_REQUEST_CODE);
}
});
and a little lower
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i(TAG, "Entered onActivityResult()");
String input=data.getStringExtra(TAG);
mUserTextView.setText(input);
}
This is activity 2 after user enters some data
String input=mEditText.getText().toString();
Intent i = new Intent(ExplicitlyLoadedActivity.this, ActivityLoaderActivity.class);
i.putExtra("TAG",input);
startActivity(i);
this.setResult(RESULT_OK);
finish();
No error messages at all but the text on screen doesnt update. it is supposed to
don't need start activity in second class:
you need change your code with:
Intent i = new Intent(); // or // Intent i = getIntent()
i.putExtra("TAG",input);
setResult(RESULT_OK , i);
finish();
and for cancel that,
setResult(RESULT_CANCELED, i);
finish();
On your Activity2 you are launching a new instance of ExplicityLoadedActivity instead of returning into the previous instance.
You should only set the result, and finish your second activity.
Here's the code you can try on your 2nd activity:
Intent returnIntent = new Intent();
returnIntent.putExtra("TAG",input);
setResult(RESULT_OK, returnIntent);
finish();
try like this
String input=data.getStringExtra("TAG");
in place of
String input=data.getStringExtra(TAG);
Try like this to set result code in ExplicitlyLoadedActivity
String input=mEditText.getText().toString();
Intent i = new Intent();
i.putExtra("TAG",input);
this.setResult(RESULT_OK,i);
finish();
and in ActivityLoaderActivity access that result string i.e.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i(TAG, "Entered onActivityResult()");
String input=data.getStringExtra("TAG");
mUserTextView.setText(input);
}
Related
I am trying to pass data backwards to an activity, however I can never get my onActivityResult function to be called. When I start the new activity, I create a new intent like regular
Intent intent = new Intent(this, NewLoanCost.class);
intent.putExtra("defaultsArray", jDefaultsArray.toString());
intent.putExtra("loanSelection", loanSelection);
intent.putExtra("buyerSellerSelection", buyerSellerSelection);
startActivity(intent);
and when I want to go back to the previous activity, I am overriding the back button to create a new intent and store the data
#Override
public void onBackPressed() {
Intent intent = new Intent();
intent.putExtra("fullCosts", fullCosts.toString());
setResult(RESULT_OK, intent);
super.onBackPressed();
}
but in the first activity, I can't even get a debugging toast to appear. Am I missing something blatant?
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
//super.onActivityResult(requestCode, resultCode, data);
Toast.makeText(this, "onActivityResult", Toast.LENGTH_SHORT).show();
if (requestCode == 1) {
if(resultCode == RESULT_OK){
try {
jDefaultsArray = new JSONArray(data.getStringExtra("fullCosts"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
To receive a result you should use startActivityForResult instead of startActivity
Change your code to
Intent intent = new Intent(this, NewLoanCost.class);
intent.putExtra("defaultsArray", jDefaultsArray.toString());
intent.putExtra("loanSelection", loanSelection);
intent.putExtra("buyerSellerSelection", buyerSellerSelection);
startActivityForResult(intent, 1);
the second parameter is an int with the request code you will use in onActivityResult
Use startActivityForResult.
Replace your code with this, then it should work:
Intent intent = new Intent(this, NewLoanCost.class);
intent.putExtra("defaultsArray", jDefaultsArray.toString());
intent.putExtra("loanSelection", loanSelection);
intent.putExtra("buyerSellerSelection", buyerSellerSelection);
startActivityForResult(intent);
I can send data from the first activity but on repeating the same procedure on the second activity to send data to ble device is not successful. How can I send data from second activity?
use this to save
Intent intent = new Intent(FirstScreen.this, SecondScreen.class)
intent .putExtra(strName, keyIdentifer );
use this to get
String newString;
if (savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
if(extras == null) {
newString= null;
} else {
newString= extras.getString("STRING_I_NEED");
}
} else {
newString= (String) savedInstanceState.getSerializable("STRING_I_NEED");
}
If you just want to send data to the next activity, use
Intent intent = new Intent(FirstActivity.this, SecondActivity.class)
intent.putExtra("id_for_value", value);
startActivity(intent);
And recover it with
value= getIntent().getExtras().getString("id_for_value");//if it is a string
OR
If you want to send data back from the Second activity back to the Previous, you have to use start activity for results
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
startActivityForResult(intent, 2)//where 2 is the request code
finish();
Again in the FirstActivity, overide this
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
// check if the request code is same as what is passed here it is 2
if(requestCode==2)
{
String result=data.getStringExtra("ResultId");
}
}
And in your PreviousActivity, you pass the data like this
Intent intent=new Intent();
intent.putExtra("ResultId",message);
setResult(2,intent);
finish();
I have two activities "A" and "B".
In my "A" activity I use startActivityForResult:
Intent i = new Intent(A.this, B.class);
setResult(RESULT_OK, i);
startActivityForResult(i, 121245);
finish();
This is the code of my "B" activity:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
Toast.makeText(B.this, "onActivityResult", Toast.LENGTH_SHORT).show();
if(requestCode == 121245) {
if (resultCode == RESULT_OK)
//make something
}
}
Why my Toast doesn't show?
onActivityResult will be called on the activity which is starting the activity for result, meaning calls the startActivityForResult method
What that means is if you want to be notified when the B activity finishes in A activity, you would first in A activity start the B activity like you did in your example code
Intent i = new Intent(A.this, B.class);
setResult(RESULT_OK, i);
startActivityForResult(i, 121245);
then when B activity finishes A activity's onActivityResultis called and there you can do whatever you want.
Here's a diagram if it helps you understand the flow of the application
You have a little mistake
at A class should be:
Intent i = new Intent(A.this, B.class);
startActivityForResult(i, 121245);
at B class to return:
Intent i = new Intent();
setResult(RESULT_OK, i);
finish();
and handle it at A
#Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
Toast.makeText(B.this, "onActivityResult", Toast.LENGTH_SHORT).show();
if(requestCode == 121245) {
if (resultCode == RESULT_OK)
//make something
}
}
onActivityResult()
must be the part of your A activity, Your B activity will return some data back to A and then onActivityResult() will be called.
Suppose I have 3 activities.
First
Second
Result.
First and Second both call Result activity by startActivityForResult().
In Result, on basis of calling activity, I want to return different results.
Is there any way to decide which one has called Result Activity and return result to that activity.
You can find the calling activity using getCallingActivity ()
You can't have multiple activities on top at the same time. Are you trying to have them run in order, one after the other?
One way to accomplish this is to start each activity for result:
Intent intent = new Intent(this, MyActivity.class);
startActivityForResult(intent, 0);
Where you use the request code to track when activity is running. Then, in onActivityResult you can start the next one:
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
if (requestCode < NUM_ACTIVITIES) {
Intent intent = new Intent(this, MyActivity.class);
startActivityForResult(intent, requestCode + 1);
}
}
If you want to have some of the activities immediatly in the background, you can chain them together by calling startActivity in each Activity's onCreate. If you start a new Activity in onCreate before creating any views, the activity will never be visible.
protected void onCreate (Bundle savedInstanceState) {
int numLeft = getIntent().getIntExtra("numLeft");
if (numLeft > 0) {
Intent intent = new Intent(this, MyActivity.class);
intent.putExtra("numLeft", numLeft - 1);
startActivity(intent);
}
}
In you calling activity finish the activity with setresult
Intent in = new Intent();
setResult(100, in);
finish();
In your first activity
#Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
Log.i(Tag, "Result: " + Integer.toString(resultCode));
if (resultCode == 200) {
Log.i(Tag, "second");
} else if (resultCode == 100) {
Log.i(Tag, "activity");
}
}
sorry my english is no good
i want when user click button, a new activity will starts and return data , i make this
first activity
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivityForResult(new Intent(EditCustomerProfile.this, Address.class), 1);
}
});
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
String s = data.getData().toString();
EditText et = (EditText)findViewById(R.id.et_edit_customer_profile_address);
et.setText(s);
}
second activity
Intent resultsIntent = new Intent();
String data = "adsfasd";
resultsIntent.setData(Uri.parse(data));
setResult(RESULT_OK, resultsIntent);
finish();
it work good, i want not just send one string, i want to send 3 strings like "city", "street", "home" , are there a way to return labeled data like
intent.setdata("city", "roma");
intent.setdata("street", "colicano");
Yes of course you can do it. Just place the key/value in intent by using putExtra()
Example:
Intent resultsIntent = new Intent();
resultsIntent .putExtra("city", "Ahmedabad");
resultsIntent .putExtra("street", "Ahmedabad");
resultsIntent .putExtra("home", "India");
setResult(RESULT_OK, resultsIntent);
finish();
Just use this instead
intent.putExtra("city", "roma");
intent.putExtra("street", "colicano");