user1958789
user1958789

Reputation: 75

Android Start Activity for Result

I'm a total newbie to android. I want to send user input from one activity to another, when I enter text and hit the sendText button I return to the main activity but I am getting 'null' in the textview where the result should be. Have looked at other answers but still no joy!

Main activity, receives user input from other activity

TextView textView1;
     public class MainActivity extends Activity {

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

    textView1 = (TextView) findViewById(R.id.textView1);


   }
    public void relativeLayout(View view)
   {
   // Create The  Intent and Start The Activity to get The message

      Intent relativeLayoutIntent=new Intent(MainActivity.this,ExplicitRelativeLayout.class);         
    startActivityForResult(relativeLayoutIntent, 2);
   }
     @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)
    {
        if(null!=data)
        {
            // fetch the message String
            String name = data.getStringExtra("name");
            String email = data.getStringExtra("email");
            String phone = data.getStringExtra("phone");

            // Set the message string in textView
            textView1.setText("Name: " + name + ""  + "\n" + "Email: " + email + " " + "\n" +"Phone:" + phone );
        }

RelativeLayout Activity, user inputs text

public class ExplicitRelativeLayout extends AppCompatActivity {


   EditText setName;
   EditText setEmail;
   EditText setPhone;


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

    // Get the reference of Edit Text
    setName = (EditText) findViewById(R.id.setName);
    setEmail = (EditText) findViewById(R.id.setEmail);
    setPhone = (EditText) findViewById(R.id.setPhone);
}


public void sendText (View views)
{
    // get the Entered  message
    String name=setName.getText().toString();
    String email= setEmail.getText().toString();
    String phone=setPhone.getText().toString();
    Intent intentmessage = new Intent();

    // put the message in Intent
    intentmessage.putExtra("Name:",name);
    intentmessage.putExtra("Email:",email);
    intentmessage.putExtra("Phone:",phone);

    // Set The Result in Intent
    setResult(2,intentmessage);

    // finish The activity
    finish();
}
}

Upvotes: 2

Views: 111

Answers (5)

R_K
R_K

Reputation: 1023

Change your code of ExplicitRelativeLayout activity ::

  1. setResult(RESULT_OK,intent);
  2. keep same key on both activity.
 public void sendText (View views){
            // get the Entered  message
            String name=setName.getText().toString();
            String email= setEmail.getText().toString();
            String phone=setPhone.getText().toString();
            Intent intentmessage = new Intent();

            // put the message in Intent
            intentmessage.putExtra("name:",name);
            intentmessage.putExtra("email:",email);
            intentmessage.putExtra("phone:",phone);

            // Set The Result in Intent
            setResult(RESULT_OK,intentmessage);

            // finish The activity
            finish();
        }

Upvotes: 0

Skizo-ozᴉʞS ツ
Skizo-ozᴉʞS ツ

Reputation: 20626

The problem is when you create the keyString take a look

//put the message in Intent
intentmessage.putExtra("Name:",name);
intentmessage.putExtra("Email:",email);
intentmessage.putExtra("Phone:",phone);

On this putExtra the keyString is Name:, Email: and Phone:, and you are trying to get the strings with this keyString

// fetch the message String
String name = data.getStringExtra("name");
String email = data.getStringExtra("email");
String phone = data.getStringExtra("phone");

You are looking for a name as a keyString but there is nothing on it, cause you decided to call it Name:

Take a look on putExtra(String name, Bundle value);

The string is :

The name of the extra data, with package prefix.

And the value is :

The Bundle data value.

Hope it helps.

Upvotes: 3

Remees M Syde
Remees M Syde

Reputation: 2609

It seems there is a mismatch in your keys your using in two classes

Use intentmessage.putExtra("name",name);

Instead of intentmessage.putExtra("Name:",name);

Upvotes: 1

Orkun Kocyigit
Orkun Kocyigit

Reputation: 1147

Intent class carries all of it is extra data with a HashMap structure which finds items with given id. Your problem is you are putting down different ids for each item in your two different Intent classes, for example:

In relative activity:

intentmessage.putExtra("Name:",name);

Your id is "Name:"

In your main activity:

String name = data.getStringExtra("name");

Your id is "name".

Make sure both of the id's you use in your intent classes are the same and it will work.

Upvotes: 1

MurugananthamS
MurugananthamS

Reputation: 2405

Activity B:
 @Override
public void onBackPressed() {
// TODO Auto-generated method stub

//data send to Activity B
Intent intent = new Intent();
intent.putExtra("MESSAGE", strtext + "");//your msg
setResult(2, intent);

}


  Activiy A:
  @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

    String sSuName = data.getStringExtra("MESSAGE");

    //txtfavouratecount.setText(sSuName);

}

Upvotes: 0

Related Questions