Qassim babyDroid
Qassim babyDroid

Reputation: 97

passing data from activity to activity in android

What is the problem with my code :(

This is the sending class:

public class Send extends AppCompatActivity {

String message_text;
final static String MSG_KEY = "this.is.the.message";

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

public void sendMessage(View view) {
    EditText entryText = (EditText)findViewById(R.id.message_text);
    message_text = entryText.getText().toString();
    Intent intent = new Intent(this, MainActivity.class);
    intent.putExtra(message_text, MSG_KEY);
    startActivity(intent);
}

}

this is the receiver:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TextView theMessage = (TextView)findViewById(R.id.theMessage);
    theMessage.setText(getIntent().getStringExtra("this.is.the.message"));
}

}

the app starts but it does not pass text, just an empty recieving activity ???

Upvotes: 0

Views: 51

Answers (3)

Yasin Yaqoobi
Yasin Yaqoobi

Reputation: 2040

If changing intent.putExtra(MSG_KEY, message_text); doesn't fix the issue. Check AndroidManifest.xml to make sure that MainActivity is not the launcher otherwise your MainActivity starts before the send class.

Upvotes: 0

Rohit5k2
Rohit5k2

Reputation: 18112

You are using value as key and key as value which is causing this issue.

Change

intent.putExtra(message_text, MSG_KEY);

to

intent.putExtra(MSG_KEY, message_text);

Upvotes: 0

Suleiman Dibirov
Suleiman Dibirov

Reputation: 1023

intent.putExtra(message_text, MSG_KEY);

replace with

intent.putExtra(MSG_KEY, message_text);

Frist argument is NAME , second argument - VALUE

Upvotes: 1

Related Questions