2

I am just a beginner.I am trying using uri.parse for returning the result. This is my main java file.

package com.example.returnresult;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

int request_code=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void sendMessage(View v){
    Intent i = new Intent(this,Second.class);
    startActivityForResult(i,request_code);

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent       data) {

    if(requestCode == request_code)
    {
        if(resultCode == RESULT_OK)
        {               
            Toast.makeText(this,data.getData().toString(),
                    Toast.LENGTH_SHORT).show();
        }
    }
}

}

This activity just shows a button on screen to go to second activity.And it's working fine.

And this is the second java file.

package com.example.returnresult;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class Second extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);
}


public void sendBack(View v){

    Intent i = new Intent();
    EditText txt = (EditText)findViewById(R.id.age);
    i.setData(Uri.parse(txt.getText().toString()));

    setResult(RESULT_OK,i);
    finish();
}

}

This activity shows a EditText field. Its id is age. There's a button below it that has to send back data to first activity and show a Toast. But this button is not working. I'm not able to get the problem. Please help!

  • 2
    Instead of using `Intent.setData()` and `Intent.getData()`, try using `Intent.putExtra()` and `Intent.getExtra()`. Explained well in couple of answers for http://stackoverflow.com/questions/920306/sending-data-back-to-the-main-activity-in-android – Balkrishna Rawool Jun 13 '15 at 18:10

1 Answers1

0

You seen not to have initialized the button in your second java file. If you have then you should use

i.putExtra("string data", Uri.parse(txt.getText().toString()));

instead of

i.setData(Uri.parse(txt.getText().toString()));

Also retrieve the information by

data.getStringExtra("string data")

instead of

data.getData().toString()

Hope this helps.

Ish
  • 194
  • 1
  • 2
  • 13
  • Thanks for help. what the app should do is on clicking a button it should go to second activity and return a result from there and show a toast on first activity. But as you can see even in main java file i haven't declared button just defined the function to start second activity. Similarly i have done in the second. Even if i declare the button in the second I am not using any function along with it except the onClick i.e. sendBack. Although i tried doing what you mentioned it didn't work. Any other suggestion? – Abhinav Garg Jun 15 '15 at 05:33
  • Please write few log messages in onActivityResult in MainActivity inside each if block so we can know exactly where the code is not being executed correctly. – Ish Jun 15 '15 at 05:55