-1

I had the Edit Text's in First Activity and now i want to get the string from current Activity to Second Activity. Here is the code for first Activity.

    package com.example.bookyourcomfort;

import android.R.string;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;

public class CabBooking extends Activity {
EditText source,destination;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cab_booking);
        source=(EditText) findViewById(R.id.autotxt_source);
        destination=(EditText) findViewById(R.id.auto_destination);
        String s=source.getText().toString();
        String d=destination.getText().toString();
        final Bundle basket= new Bundle();
        basket.putString("source", s);
        basket.putString("destination", d);

        Button submit=(Button) findViewById(R.id.button_submit);
        submit.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                Intent i=new Intent(CabBooking.this,Travels.class);
                i.putExtras(basket);
                startActivity(i);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.cab_booking, menu);
        return true;
    }

}

Here is the second Activity code:

package com.example.bookyourcomfort;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class Travels extends Activity {
TextView src,dest;
String source,destination;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_travels);
        src=(TextView) findViewById(R.id.txt_source);
        dest=(TextView) findViewById(R.id.txt_destination);


Bundle gt=getIntent().getExtras();
source=gt.getString("source");
src.setText(source);
destination=gt.getString("destination");
dest.setText(destination);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.travels, menu);
        return true;
    }

}

Can anyone help me?

MiguelCatalan
  • 916
  • 10
  • 26
  • atleast search first https://www.google.co.in/search?q=how+to+transfer+string+from+one+activity+to+another&ie=utf-8&oe=utf-8&gws_rd=cr&ei=6v5SVpDrGdK3uASkwIDoCQ – karan Nov 23 '15 at 11:56
  • Ya already there but i it doesn't help me? – manikanta gokavarapu Nov 23 '15 at 11:57
  • I do not get the strings to another Activity – manikanta gokavarapu Nov 23 '15 at 11:58
  • put this your bundle and string code inside onclick in first activity – karan Nov 23 '15 at 11:59
  • you should add getText() from editText inside OnClick of button. – Saritha G Nov 23 '15 at 11:59
  • Add these lines inside onClickListener of button... String s=source.getText().toString(); String d=destination.getText().toString(); final Bundle basket= new Bundle(); basket.putString("source", s); basket.putString("destination", d); – Saritha G Nov 23 '15 at 12:00
  • Thank You Very much – manikanta gokavarapu Nov 23 '15 at 12:04
  • This question is not duplicate of [How do I pass data between activities on Android?](http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-on-android) question because OP is doing everything in right way( for sending data between two Activities) but getting `Empty` Strings for both keys in Second Activity because OP is not retrieving latest user input values from EditText on Button click – ρяσѕρєя K Nov 23 '15 at 12:11

1 Answers1

2

Here:

String s=source.getText().toString();
String d=destination.getText().toString();

lines causing issue because getting user text from EditText on onCreate method which always return Empty String.

To fix current issue move :

        String s=source.getText().toString();
        String d=destination.getText().toString();
        final Bundle basket= new Bundle();
        basket.putString("source", s);
        basket.putString("destination", d);

lines inside onClick method of Button click listener.

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213