I am having a trouble in passing data between Activity2 and ActivityMain. I have ActivityMain with a "Add Person" button. When I press this button, I go to Activity2, after that I fill field Name and press button "Add", but can't to add Name on my ActivityMain.
ActivityMain.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add Person"
android:onClick="addPerson"
/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
ActivityMain.java
package com.example.test.zadanie01;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data){
Intent intent = getIntent();
String message = intent.getStringExtra(PersonActivity.EXTRA_MESSAGE);
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
setContentView(textView);
}
public void addPerson(View view){
Intent intent = new Intent(this, PersonActivity.class);
startActivityForResult(intent, 1);
}
}
Activity2.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Name"
android:id="@+id/send_name"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add Person"
android:onClick="addPersonToMain"
/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
Activity2.java
package com.example.test.zadanie01;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import static android.provider.AlarmClock.EXTRA_MESSAGE;
public class PersonActivity extends AppCompatActivity {
public final static String EXTRA_MESSAGE = "EXTRA_MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_person);
}
public void addPersonToMain(View view){
Intent returnintent = new Intent(this, MainActivity.class);
EditText SendName = (EditText) findViewById(R.id.send_name);
String message = SendName.getText().toString();
returnintent.putExtra(EXTRA_MESSAGE, message);
setResult(RESULT_OK, returnintent);
finish();
}
}
Change this part of your code.
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if (requestCode == 1){
if (resultCode == RESULT_OK){
String message = data.getStringExtra(PersonActivity.EXTRA_MESSAGE);
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
setContentView(textView);
}
}
}
You don't need to create another Intent
in onActivityResult
, the Intent is already delivered by the method: Intent data
. Then use data as your intent. And before all, check if it is the requestCode you asked and if the result is OK.
EDIT
Answering another part of the question from the comments.
1 - Add a TextView to your activity_main.xml and set its visibility to gone by default.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/text_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40sp"
android:visibility="gone" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add Person"
android:onClick="addPerson"
/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
2 - When retrieveng the data message, show the TextView and display the content.
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if (requestCode == 1){
if (resultCode == RESULT_OK){
String message = data.getStringExtra(PersonActivity.EXTRA_MESSAGE);
TextView textView = findViewById(R.id.text_message);
textView.setVisibility(View.VISIBLE);
textView.setText(message);
}
}
}
try to replace
String message = intent.getStringExtra(PersonActivity.EXTRA_MESSAGE);
with
String message = data.getStringExtra(PersonActivity.EXTRA_MESSAGE);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.