一.设计目标
1、请根据课程内容实现对Activity生命周期的理解,使用log展示生命周期的状态变化;
2、根据博客:https://www.jianshu.com/p/c4cfe38a91ed的内容(进阶篇中的 “点击”),在前次作业的基础上增加列表项的单项点击功能,具体要求是:新建一个新的activity1,recycleview的某一项点击后跳转到这个新的activity1。如:点击新闻列表会跳转到新闻详情页面;
3、实现最新的activityforresult功能,具体要求如下:新建一个新的activity2,在activity1上添加按钮可收到activity2的回传值。
二.设计流程
1. 生命周期
@Override
protected void onStart() {
super.onStart();
Log.d("this is", "onStart");
}
@Override
protected void onStop() {
super.onStop();
Log.d("this is ","onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d("this is ","onDestroy");
}
@Override
protected void onPause() {
super.onPause();
Log.d("this is ","onPause");
}
@Override
protected void onResume() {
super.onResume();
Log.d("this is ","onResume");
}
@Override
protected void onRestart() {
super.onRestart();
Log.d("this is ","onRestart");
}
2. 点击跳转
1). 新建MainActivity.java
package com.example.applicationtest;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class MainActivity2 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Intent intent = getIntent();
int a = intent.getIntExtra("jamp",1);
TextView textView = findViewById(R.id.textViewx);
textView.setText("这是用户"+(a+1));
}
}
2). myadapter中添加点击
holder.L.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
Intent intent = new Intent(context,MainActivity2.class);
intent.putExtra("jamp",position);
context.startActivity(intent);
}
});
3). 创建mainactivity2.xml,添加一个textview
<?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=".MainActivity2">
<TextView
android:id="@+id/textViewx"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="TextView"
android:textSize="30dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.5" />
</androidx.constraintlayout.widget.ConstraintLayout>
3. activityforresult实现
1). 在fragment2里放入一个button
package com.example.applicationtest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.fragment.app.Fragment;
public class Fragment2 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view;
view = inflater.inflate(R.layout.activity_main3, container, false);
Button button =view.findViewById(R.id.buttonx);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getContext(),MainActivity3.class);
test.launch(intent);
}
});
return view;
}
public ActivityResultLauncher test= registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
if(result.getResultCode()== Activity.RESULT_OK){
TextView textView=getActivity().findViewById(R.id.textViewx2);
textView.setText(result.getData().getStringExtra("turn"));
};
}
}
);
}
2). 创建mainactivity3回传输出
package com.example.applicationtest;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class MainActivity3 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
Intent intent = new Intent();
intent.putExtra("turn","你点击成功");
setResult(Activity.RESULT_OK,intent);
finish();
}
}
3). 创建mainactivity3.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=".MainActivity3">
<TextView
android:id="@+id/textViewx2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="30dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/buttonx"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="80dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
三、运行结果展示
1、
2、
3、