一.设计目标及功能
1、请根据课程内容实现对Activity生命周期的理解,使用log展示生命周期的状态变化;
2、在前次作业的基础上增加列表项的单项点击功能,具体要求是:新建一个新的activity1,recycleview的某一项点击后跳转到这个新的activity1。如:点击新闻列表会跳转到新闻详情页面;
3、实现最新的activityforresult功能,具体要求如下:新建一个新的activity2,在activity1上添加按钮可收到activity2的回传值。如:新闻详情页面中点击收藏按钮可显示当前的收藏数量为N。
二.代码
xml文件编写:
activity_item.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=".itemActivity">
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="返回的信息"
android:textColor="@color/red"
android:textSize="30sp"
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.69" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这是发现页面"
android:textSize="30sp"
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.167" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查看信息"
android:textSize="30sp"
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.345" />
</androidx.constraintlayout.widget.ConstraintLayout>
activity_result.xml
java文件:
itemActivity.java
package com.example.myapplication;
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.widget.Button;
import android.widget.TextView;
public class itemActivity extends AppCompatActivity {
private static final String TAG = "itemActivity";
private Button button;
private TextView textview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item);
Log.i(TAG, "The activity state---->onCreate");
textview=findViewById(R.id.textView3);
button=findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(itemActivity.this,ResultActivity.class);
test.launch(intent);
}
});
}
public ActivityResultLauncher test = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>(){
@Override
public void onActivityResult(ActivityResult result) {
if (result.getResultCode() == 888){
Log.d("xr","onActivityResultLauncher...");
textview.setText(result.getData().getStringExtra("data"));
}
}
});
protected void onStart() {
super.onStart();
Log.i(TAG, "The activity state---->onStart");
}
protected void onRestart() {
super.onRestart();
Log.i(TAG, "The activity state---->onRestart");
}
protected void onResume() {
super.onResume();
Log.i(TAG, "The activity state---->onResume");
}
protected void onPause() {
super.onPause();
Log.i(TAG, "The activity state---->onPause");
}
protected void onStop() {
super.onStop();
Log.i(TAG, "The activity state---->onStop");
}
protected void onDestroy() {
super.onDestroy();
Log.i(TAG, "The activity state---->onDestroy");
}
}
ResultActivity.java
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import java.util.Calendar;
public class ResultActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
Intent intent=getIntent();
intent.putExtra("data","迪迦已就绪 准备拯救世界\n"+"目前时间:\n"+ Calendar.getInstance().getTime()+"\n起飞倒计时:\n"+"08:25:36");
setResult(888,intent);
finish();
}
}
三.结果展示
1、生命周期状态变化
2.新建activity1,recycleview的某一项点击后跳转到这个activity1。点击发现的某一选项,进入到该选项的详情界面
3、新建一个新的activity2,在activity1上添加按钮可收到activity2的返回信息。
四.代码仓库地址
MyApplication: android studio (gitee.com)