一.设计要求:在上一次设计的基础上进行补充,实现activityforresult功能。
二.设计步骤
1.新建一个新的activity2,recycleview的某一项点击后跳转到这个新的activity2
activity_2.xml,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"tools:context=".Activity2"><TextViewandroid:id="@+id/textView10"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_marginTop="404dp"android:gravity="center"android:text="这是详情页面"android:textSize="30dp"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintHorizontal_bias="1.0"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" /><TextViewandroid:id="@+id/textView11"android:layout_width="0dp"android:layout_height="95dp"android:layout_marginTop="96dp"android:gravity="center"android:text="聊天记录"android:textSize="45dp"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintHorizontal_bias="0.0"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" /><Buttonandroid:id="@+id/button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="查看聊天记录"android:textSize="30dp"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintHorizontal_bias="0.498"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent"app:layout_constraintVertical_bias="0.428" /></androidx.constraintlayout.widget.ConstraintLayout>
2.编写activity通过点击button接收到resultActivity的回传值,以及观察activity的生命周期
package com.example.mywork;import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;public class Activity2 extends AppCompatActivity {private Button button;private TextView textView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);supportRequestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.activity_2);textView=findViewById(R.id.textView10);button=findViewById(R.id.button);button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {Intent intent=new Intent(Activity2.this,resultActivity.class);test.launch(intent);}});}public ActivityResultLauncher test= registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),new ActivityResultCallback<ActivityResult>() {@Overridepublic void onActivityResult(ActivityResult result) {if(result.getResultCode()==666){Log.d("lx","onActivityResult 有返回值。。。");textView.setText(result.getData().getStringExtra("data"));};}});@Overrideprotected void onStart() {super.onStart();Log.d("lx","activity2 is onStart...");}@Overrideprotected void onRestart() {super.onRestart();Log.d("lx","activity2 is onRestart...");}@Overrideprotected void onStop() {super.onStop();Log.d("lx","activity2 is onStop...");}@Overrideprotected void onDestroy() {super.onDestroy();Log.d("lx","activity2 is onDestroy...");}@Overridepublic void finish(){super.finish();Log.d("lx","activity2 is finish...");}@Overridepublic void finishActivity(int requestCode){super.finishActivity(requestCode);Log.d("lx","finishActivity2...");}
}
2.新建一个新的resultActivity 完成向activity2传值的功能
package com.example.mywork;import androidx.appcompat.app.AppCompatActivity;import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;import java.util.Calendar;public class resultActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_result);Intent intent =getIntent();intent.putExtra("data","今天是xr老师讲授的安卓课。\n"+Calendar.getInstance().getTime());setResult(Activity.RESULT_OK,intent);finish();}
}
制作效果