返回上一个活动只需要按以下Back键就可以了,并且使用startActivityForResult(),这个方法也是用于启动活动的,而且会返回一个结果给上一个活动。
startActivityForResult()方法接收两个参数,第一个参数是Intent,第二个参数是请求码,用于在之后回调中判断数据的来源。
以下为例子:
首先你需要两个可以互传的Activity ,这里,我们以MainActivity和SecondActivity为例:
MainActivity的Layout如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:background="#ffffff" tools:context=".MainActivity"> <EditText android:id="@+id/edt" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="top" android:minLines="3" android:maxLines="3" android:textColor="#000000" android:textSize="20dp" android:hint="请输入.." android:textColorHint="#707070"/> <Button android:id="@+id/btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="点击" android:textSize="15dp" android:textColor="#000000"/> <TextView android:id="@+id/txt_first" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="变变变" android:textColor="#000000" android:textSize="20dp" android:layout_marginTop="10dp"/>
SecondActivity的Layout如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:background="#ffffff" android:orientation="vertical" tools:context=".SecondActivity"> <TextView android:id="@+id/txt" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20dp" android:textColor="#000000"/> <Button android:id="@+id/btn_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20dp" android:textColor="#000000" android:text="点吧" android:layout_marginTop="10dp"/> </LinearLayout>
接着,我们要让SecondActivity在点击了按钮btn_1之后返回数据到MainActivity:
import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.SearchView; import android.widget.TextView; import java.io.BufferedWriter; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; public class MainActivity extends AppCompatActivity { private EditText edt; private Button btn; private TextView txt; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); edt = findViewById(R.id.edt); btn = findViewById(R.id.btn); txt = findViewById(R.id.txt_first); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String data = edt.getText().toString() ; Intent intent = new Intent(MainActivity.this,SecondActivity.class); intent.putExtra("extra_data",data); startActivityForResult(intent,1); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); switch(requestCode){ case 1: if(resultCode == RESULT_OK){ String returnedData = data.getStringExtra("return_data"); txt.setText(returnedData); } break; default: } } }
然后是SecondActivity的代码:
import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; public class SecondActivity extends AppCompatActivity { private TextView txt2; private Button btn_2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); Intent intent = getIntent(); String data= intent.getStringExtra("extra_data"); txt2 = findViewById(R.id.txt); btn_2=findViewById(R.id.btn_1); txt2.setText(data); btn_2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(SecondActivity.this,MainActivity.class); intent.putExtra("return_data","This is the return of Second Activity."); setResult(RESULT_OK,intent); finish(); } }); } }