场景:第一个界面的数据可传递至第二界面;第二个界面关闭后其数据可回传至第一界面
布局:两个界面均有一个EditText & TextView 用于数据输入及显示
first activity & second activity UI:
本场景数据回传使用Intent进行通信:
[1]当仅需要一个activity向另一个activity传递数据而不用回传时,使用startIntent()即可
[2]若需要数据回传则需要startIntentForResult(),然后重写onActivityResult()即可获取
step:
[当传递内容为空时显示null]
------------------------------------------------------源码----------------------------------------------------
activity_main:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:paddingLeft="16dp"
android:paddingTop="16dp"
android:paddingRight="16dp"
android:paddingBottom="16dp">
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:layout_marginBottom="16dp"
android:src="@drawable/aa" />
<TextView
android:id="@+id/tvFirstContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:gravity="center"
android:textSize="20sp" />
<EditText
android:id="@+id/etFirstContent"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="30dp" />
<Button
android:id="@+id/btFirst"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="go next"
android:textColor="#000000"
android:textSize="18sp"
android:background="#ffffff"/>
</LinearLayout>
activity_next:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:paddingLeft="16dp"
android:paddingTop="16dp"
android:paddingRight="16dp"
android:paddingBottom="16dp">
<ImageView
android:layout_width="300dp"
android:layout_height="200dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:layout_marginBottom="16dp"
android:src="@drawable/bb" />
<TextView
android:id="@+id/tvSecondContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:textSize="20sp" />
<EditText
android:id="@+id/etSecondContent"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="30dp" />
<Button
android:id="@+id/btSecond"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="back"
android:textColor="#000000"
android:textSize="18sp"
android:background="#ffffff"/>
</LinearLayout>
MainActivity:
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import static com.tzbc.applicationtest.NextActivity.KEY_SECOND_EDIT_CONTENT;
/**
* Created by 桃子不出 on 2020/5/6.
*
* @author tzbc
*/
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = "MainActivity";
private static final int MSG_UPDATE_UI = 1124;
public static final String KEY_FIRST_EDIT_CONTENT = "key_first_edit_content";
public static final int CONTENT_REQUEST_CODE = 1119;
private TextView textView;
private EditText editText;
private Button button;
//记录et最后一次输入内容
private String lastEditContent;
private Handler handler = new Handler() {
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
if (msg.what == MSG_UPDATE_UI) {
textView.setText(msg.obj.toString());
}
}
};
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//控件初始化
editText = findViewById(R.id.etFirstContent);
textView = findViewById(R.id.tvFirstContent);
button = findViewById(R.id.btFirst);
//监听事件
editText.addTextChangedListener(textWatcher);
button.setOnClickListener(this);
}
private TextWatcher textWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Log.e(TAG, "current text: " + s);
}
@Override
public void afterTextChanged(Editable s) {
lastEditContent = s.toString();
}
};
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btFirst:
goNextActivity();
break;
default:
break;
}
}
/**
* 跳转到第二个activity
*/
private void goNextActivity() {
if (TextUtils.isEmpty(lastEditContent)) {
Log.e(TAG, "传入数据为空");
lastEditContent = "null";
}
Intent intent = new Intent(this, NextActivity.class);
intent.putExtra(KEY_FIRST_EDIT_CONTENT, lastEditContent);
startActivityForResult(intent, CONTENT_REQUEST_CODE);
}
/**
* 回调返回监听
*
* @param requestCode CONTENT_REQUEST_CODE
* @param resultCode RESULT_OK
* @param data data
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CONTENT_REQUEST_CODE && resultCode == RESULT_OK) {
if (data == null) {
Log.e(TAG, "data is null");
return;
}
String result = data.getStringExtra(KEY_SECOND_EDIT_CONTENT);
if (TextUtils.isEmpty(result)) {
Log.e(TAG, "回传数据为空");
result = "null";
}
//刷新UI
Message message = Message.obtain();
message.what = MSG_UPDATE_UI;
message.obj = result;
handler.sendMessage(message);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
//资源释放
handler.removeCallbacksAndMessages(null);
editText.removeTextChangedListener(textWatcher);
}
}
NextActivity:
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import static com.tzbc.applicationtest.MainActivity.KEY_FIRST_EDIT_CONTENT;
/**
* Created by 桃子不出 on 2020/5/6.
*
* @author tzbc
*/
public class NextActivity extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = "NextActivity";
public static final String KEY_SECOND_EDIT_CONTENT = "key_second_edit_content";
private TextView textView;
private EditText editText;
private Button button;
//tv的显示内容,初始化为空
private String currentTextContent = "";
//记录et最后一次输入内容
private String lastEditContent;
/**
* 初始化获取上一个activity传过来的数据
*
* @param intent intent
*/
private void getIntentData(Intent intent) {
if (intent == null) {
Log.e(TAG, "intent is null");
return;
}
currentTextContent = intent.getStringExtra(KEY_FIRST_EDIT_CONTENT);
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_next);
//控件初始化
editText = findViewById(R.id.etSecondContent);
textView = findViewById(R.id.tvSecondContent);
button = findViewById(R.id.btSecond);
getIntentData(getIntent());
textView.setText(currentTextContent);
//监听事件
editText.addTextChangedListener(textWatcher);
button.setOnClickListener(this);
}
private TextWatcher textWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Log.e(TAG, "current text: " + s);
}
@Override
public void afterTextChanged(Editable s) {
lastEditContent = s.toString();
}
};
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btSecond:
backActivity();
break;
default:
break;
}
}
@Override
public void onBackPressed() {
backActivity();
}
/**
* 返回上一个Activity
*/
private void backActivity() {
Intent i = new Intent();
i.putExtra(KEY_SECOND_EDIT_CONTENT, lastEditContent);
setResult(RESULT_OK, i);
finish();
}
@Override
protected void onDestroy() {
super.onDestroy();
editText.removeTextChangedListener(textWatcher);
}
}