I am trying to get a text view to update after a user has typed in a string from another result. When using this method below I get what appears to be the edit text's code address. Something like "android.widget.Edittext(b142f388 etc." regardless of what the user types in on the other activity. What am I missing here?
String collected from user:
private void enterClicked() {
Log.i(TAG,"Entered enterClicked()");
//Save user provided input from the EditText field
String result = mEditText.toString();
//Create a new intent and save the input from the EditText field as an extra
Intent i = new Intent(ExplicitlyLoadedActivity.this, ActivityLoaderActivity.class);
i.putExtra("RESULT_STRING", result);
//Set Activity's result with result code RESULT_OK
setResult(RESULT_OK, i);
//Finish the Activity
finish();
}
Activity Result:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i(TAG, "Entered onActivityResult()");
// RESULT_OK result code and a recognized request code
// If so, update the Textview showing the user-entered text.
if ( resultCode == RESULT_OK){
if(requestCode == GET_TEXT_REQUEST_CODE){
String userData = data.getStringExtra("RESULT_STRING");
mUserTextView.setText(userData);
}
}
}
String result = mEditText.getText().toString();
First apply this and check result
You need to use getText() method to get text from editText:
Try using this code: String result = mEditText.getText().toString();
private void enterClicked() {
Log.i(TAG,"Entered enterClicked()");
//Save user provided input from the EditText field
String result = mEditText.getText().toString();
//Create a new intent and save the input from the EditText field as an extra
Intent i = new Intent(ExplicitlyLoadedActivity.this, ActivityLoaderActivity.class);
i.putExtra("RESULT_STRING", result);
//Set Activity's result with result code RESULT_OK
setResult(RESULT_OK, i);
//Finish the Activity
finish();
}
Instead :
private void enterClicked() {
Log.i(TAG,"Entered enterClicked()");
//Save user provided input from the EditText field
String result = mEditText.toString();
//Create a new intent and save the input from the EditText field as an extra
Intent i = new Intent(ExplicitlyLoadedActivity.this, ActivityLoaderActivity.class);
i.putExtra("RESULT_STRING", result);
//Set Activity's result with result code RESULT_OK
setResult(RESULT_OK, i);
//Finish the Activity
finish();
}
Related
I searched a lot and tried things but the onActivityResult function isn't launched when the intent from which I try to get a string information is closed.
I use Visual Studio to write this application, this is my code :
The click event that opens the activity where users can type strings :
private void Btn_Valid_Click(object sender, EventArgs e)
{
----
Intent intent = new Intent(this, typeof(activity_OF_TransfertChxChmb));
StartActivityForResult(intent, 0);
----
}
The function in the openned Intent that should return the string information :
private void Validate()
{
string stringToPassBack = tb_Store.Text;
// put the String to pass back into an Intent and close this activity
Intent intent = new Intent();
intent.PutExtra("result", stringToPassBack);
SetResult(Result.Ok, intent);
Finish();
}
And the onActivityResult function that should be launched in the first activity:
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
base.onActivityResult(requestCode, 0, data);
if (requestCode == 0)
{
if (resultCode == -1) // Ok
{
string result = data.GetStringExtra("result");
}
if (resultCode == 0) // Canceled
{
//Write your code if there's no result
}
}
}
I am missing something, but can't figure out what.
Thank you for your help.
You are adding the extra via:
intent.PutExtra(Intent.ExtraText, stringToPassBack);
Your key is Intent.ExtraText.
You are retrieving the extra via:
string result = data.GetStringExtra("result");
Your key is "result".
So, perhaps Intent.ExtraText does not equal "result". You need to use the same key in both places.
In onActivityResult(), getIntent().getSerializableExtra() is null. But when I check the putSerializable() value, it is not null, and the tag is the same. But in iputTextActivity, I receive the valid value. Why?
class daygramActivity:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode!= Activity.RESULT_OK)
return;
if(requestCode == 1){
if(data==null){
return;
}
Message msg = (Message) getIntent().getSerializableExtra(iputTextActivity.IPUT_TEXT_RETURN_CONTENT);
if(msg == null) //I receive null
return;
updateData(1);
}
}
class iputTextActivity:
final Message msg = (Message) getIntent().getSerializableExtra(daygramActivity.SER_KEY); //I receive valid //value
mDoneButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
EditText edT = (EditText) findViewById(R.id.editText);
String input = edT.getText().toString();
Intent data=new Intent();
Bundle mBundle = new Bundle();
msg.setContent(input);
mBundle.putSerializable(IPUT_TEXT_RETURN_CONTENT,msg);
data.putExtras(mBundle);
setResult(Activity.RESULT_OK, data);
finish();
}
});
Try using the Intent passed to onActivityResult() instead of calling getIntent(). When you call getIntent() in an Activity it returns the Intent used to start that Activity. In this case by calling getIntent() it will return a reference to the Intent that was used to start your daygramActivity Activity. It will not return the Intent you used when you called setResult(). Instead, the Intent used when you call setResult() is made available to you as the Intent parameter in onActivityResult().
So in onActivityResult() change this:
Message msg = (Message) getIntent().getSerializableExtra(iputTextActivity.IPUT_TEXT_RETURN_CONTENT);
to this:
Message msg = (Message) data.getSerializableExtra(iputTextActivity.IPUT_TEXT_RETURN_CONTENT);
I'm trying to return an EditText that has one Activity to a first Activity but when I push the button to finish de second Activity and onActivityResult is call the TextView that should show the text in the EditText save, no shos nothing.
Here's the Intent creation and the starts of the second Activity:
Intent myInt = new Intent(ActivityLoaderActivity.this, ExplicitlyLoadedActivity.class);
startActivityForResult(myInt, GET_TEXT_REQUEST_CODE);
Here's how I save the EditText in ExplicitlyLoadedActivity.java:
private void enterClicked() {
Log.i(TAG,"Entered enterClicked()");
// TODO - Save user provided input from the EditText field
Editable res = mEditText.getText();
// TODO - Create a new intent and save the input from the EditText field as an extra
Intent returnIntent = new Intent();
returnIntent.putExtra("result", res);
// TODO - Set Activity's result with result code RESULT_OK
setResult(RESULT_OK,returnIntent);
// TODO - Finish the Activity
finish();
}
And here's is the code of onActivityResult:
if (requestCode == 1) {
if(resultCode == RESULT_OK){
String res = data.getStringExtra("result");
mUserTextView.setText(res);
}
}
In the debug mode, I can see that res has the text that I put in the EditText but it don't shos nothing in the aplication.
This is the code of mUserTextView:
private TextView mUserTextView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_loader_activity);
// Get reference to the textView
mUserTextView = (TextView) findViewById(R.id.textView1);
This is the full code of onActivityResult():
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i(TAG, "Entered onActivityResult()");
// TODO - Process the result only if this method received both a
// RESULT_OK result code and a recognized request code
// If so, update the Textview showing the user-entered text.
if (requestCode == 1) {
if(resultCode == RESULT_OK){
String res = data.getStringExtra("result");
mUserTextView.setText(res);
}
}
}
if (requestCode == 1) {
if(resultCode == RESULT_OK){
String res = getIntent.getStringExtra("result");
mUserTextView.setText(res);
}
}
try that piece of code
and make sure GET_TEXT_REQUEST_CODE==1
I've tried the same code as you describe above, it works well. Since res is really received in onActivityResult you should check your mUserTextView related code.
In my project i will use voice recognition. In the codes of voice recognition, an arraylist returns on activity result. But i just want to take the first data in the list. i mean in the project users will use speak button, speak and then see the results. And then there will be continue button that shows the first data of the results. How can i pass the result parameter to another activity class. But not as the originally list, just a string.
I use this code
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {
ArrayList<String> matches = new ArrayList<String>();
matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
if(matches.lenght()>0) {
result = matches.get(0)
//Time to create the new intent to the other activity
Intent i = new Intent (Activity1.this, Activity2.class);
i.putExtra("RESULT", result); //RESULT is the key
startActivityForResult(i,ID); // or startActivity(i)
}
}
super.onActivityResult(requestCode, resultCode, data);
}
Then in the other activity you must to uget the bundle and extrat the string
Bundle bundle = getIntent().getExtras();
if( (bundle!=null) && (bundle.contains("RESULT"){
String text = bundle.getString("RESULT");
}
else{
//other things
return;
}
This question already has answers here:
How to pass a value from one Activity to another in Android? [duplicate]
(7 answers)
Closed 9 years ago.
In Activity1, I input some data like name and address. When I click the next button, there will be another input form. What I want to do is, when I click BACK, I will return to Activity1 and the data I entered there previously is shown.
HELP please :)
=============
UPDATED: Activity1
private void startActivityForResult()
{
TextView textname = (TextView) findViewById(R.id.username);
TextView textaddress = (TextView) findViewById(R.id.useraddress);
Intent intent = new Intent(this, GetInformation.class);
//intent.putExtras(getIntent());
intent.putExtra("username", textname.getText().toString());
intent.putExtra("useradd", textaddress.getText().toString());
startActivityForResult(intent, 0);
}
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
TextView textname = (TextView) findViewById(R.id.username);
TextView textaddress = (TextView) findViewById(R.id.useraddress);
textname.setText(data.getStringExtra("returnname").toString());
textaddress.setText(data.getStringExtra("returnadd").toString());
}
Activity2
private void startActivityForResult()
{
final String username;
final String useraddress;
Intent intent = getIntent();
//intent.putExtras(getIntent());
username = getIntent().getStringExtra("username");
useraddress = getIntent().getStringExtra("useradd");
intent.putExtra("returnname", username);
intent.putExtra("returnadd", useraddress);
setResult(0, intent);
}
There's a simple way to do this in Android : startActivityForResult. Basically, when you launch the activity, you say that you are expecting a result. The other activity can then add information that will be returned to the starting activity. Here's a very simple code sample from the official doc :
public class MyActivity extends Activity {
...
static final int PICK_CONTACT_REQUEST = 0;
protected boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
// When the user center presses, let them pick a contact.
startActivityForResult(
new Intent(Intent.ACTION_PICK,
new Uri("content://contacts")),
PICK_CONTACT_REQUEST);
return true;
}
return false;
}
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (requestCode == PICK_CONTACT_REQUEST) {
if (resultCode == RESULT_OK) {
// A contact was picked. Here we will just display it
// to the user.
startActivity(new Intent(Intent.ACTION_VIEW, data));
}
}
}
}
You can get a much more complete description of all this on the Activity page in the official doc (section Starting Activities and Getting Results).
Save Activity1 state in method onSaveInstanceState, and then in method
void onCreate(Bundle savedInstanceState)
you can restore state, using savedInstanceState.
Or, if you want pass entered data to second activity, you can place data in intent.
Sample:
Intent i = new Intent(FirstActivity.this, SecondActivity.class);
i.putExtra("Key", "Value");
startActivityForResult(i, 0);
in Second Activity you can get data:
getIntent().getStringExtra("Key");
To return result from second activity:
Intent data = new Intent();
data.put("key", "value");
setResult(RESULT_OK, data);
then you can retrieve data in first activity using
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
data.getStringExtra("key");
}