一、实验目标
1、请根据课程内容实现对Activity生命周期的理解,使用log展示生命周期的状态变化;
2、根据博客:
https://www.jianshu.com/p/c4cfe38a91ed(进阶篇中的 “点击”) ,
在前次作业的基础上增加列表项的单项点击功能,具体要求是:新建一个新的activity1,recycleview的某一项点击后跳转到这个新的activity1。如:点击新闻列表会跳转到新闻详情页面;
3、实现最新的activityforresult功能,具体要求如下:新建一个新的activity2,在activity1上添加按钮可收到activity2的回传值。如:新闻详情页面中点击收藏按钮可显示当前的收藏数量为N。
二、实验过程
2.1布局文件包含Activity_1.xml和Activity_main.xml
2.1.1Activity_1.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=".activity1">
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="联系人"
android:textSize="30dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView9"
android:layout_width="75dp"
android:layout_height="51dp"
android:layout_marginStart="16dp"
android:text="消息"
android:textSize="30dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.14" />
<Button
android:id="@+id/buttonCollect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="96dp"
android:text="Collect"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.589"
app:layout_constraintStart_toEndOf="@+id/textView9"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/backText"
android:layout_width="193dp"
android:layout_height="74dp"
android:text="Collected Times:0"
android:textSize="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.792"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/buttonCollect" />
</androidx.constraintlayout.widget.ConstraintLayout>
2.1.2Activity_main.xml
<?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"
tools:context=".MainActivity">
<include layout="@layout/titlebar" />
<FrameLayout
android:id="@+id/Frame_all"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</FrameLayout>
<include layout="@layout/button_layout" />
</LinearLayout>
2.2 java文件包含activity和adpter适配器
2.2.1Mainactivity
package com.example.homework;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private Fragment weixinFragment=new Fragment_weixin();
private Fragment tongxunluFragment=new Fragment_tongxunlu();
private Fragment faxianFragment=new Fragment_faxian();
private Fragment woFragment=new Fragment_wo();
private FragmentManager manager;
//private FragmentTransaction transaction;
private ImageButton weixinbutton;
private ImageButton tongxunlubutton;
private ImageButton faxianbutton;
private ImageButton wobutton;
private LinearLayout Linearlayout1,Linearlayout2,Linearlayout3,Linearlayout4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Linearlayout1=findViewById(R.id.LinearLayout1);
Linearlayout2=findViewById(R.id.LinearLayout2);
Linearlayout3=findViewById(R.id.LinearLayout3);
Linearlayout4=findViewById(R.id.LinearLayout4);
Linearlayout1.setOnClickListener(this::onClick);
Linearlayout2.setOnClickListener(this::onClick);
Linearlayout3.setOnClickListener(this::onClick);
Linearlayout4.setOnClickListener(this::onClick);
weixinbutton= (ImageButton) findViewById(R.id.img_weixin);
tongxunlubutton= (ImageButton) findViewById(R.id.img_tongxunlu);
faxianbutton= (ImageButton) findViewById(R.id.img_faxian);
wobutton= (ImageButton) findViewById(R.id.img_wo);
Frags_init();
Frags_show(0);
}
private void Frags_init()
{
// transaction=manager.beginTransaction()
// .add(R.id.Frame_all,weixinFragment)
// .add(R.id.Frame_all,tongxunluFragment)
// .add(R.id.Frame_all,faxianFragment)
// .add(R.id.Frame_all,woFragment);
// transaction.commit();
manager=getSupportFragmentManager();
FragmentTransaction transaction= manager.beginTransaction();
transaction.add(R.id.Frame_all,weixinFragment);
transaction.add(R.id.Frame_all,tongxunluFragment);
transaction.add(R.id.Frame_all,faxianFragment);
transaction.add(R.id.Frame_all,woFragment);
transaction.commit();
}
private void Frags_hide(FragmentTransaction transaction)
{
// transaction=manager.beginTransaction()
// .hide(weixinFragment)
// .hide(tongxunluFragment)
// .hide(faxianFragment)
// .hide(woFragment);
// transaction.commit();
transaction.hide(weixinFragment);
transaction.hide(tongxunluFragment);
transaction.hide(faxianFragment);
transaction.hide(woFragment);
}
private void Frags_show(int i)
{
FragmentTransaction transaction=manager.beginTransaction();
Frags_hide(transaction);
switch(i){
case 0:
Log.d("Show Fragment","1");
transaction.show(weixinFragment);
weixinbutton.setImageResource(R.drawable.weixin_b);
break;
case 1:
transaction.show(tongxunluFragment);
tongxunlubutton.setImageResource(R.drawable.tongxunlu_b);
break;
case 2:
transaction.show(faxianFragment);
faxianbutton.setImageResource(R.drawable.faxian_b);
break;
case 3:
transaction.show(woFragment);
wobutton.setImageResource(R.drawable.wo_b);
break;
default:
break;
}
transaction.commit();
}
public void onClick(View v){
Log.d("onClick","1");
reFresh();
switch(v.getId()){
case R.id.LinearLayout1:
Frags_show(0);
break;
case R.id.LinearLayout2:
Frags_show(1);
break;
case R.id.LinearLayout3:
Frags_show(2);
break;
case R.id.LinearLayout4:
Frags_show(3);
break;
default:
break;
}
}
public void reFresh(){
weixinbutton.setImageResource(R.drawable.weixin);
tongxunlubutton.setImageResource(R.drawable.tongxunlu);
faxianbutton.setImageResource(R.drawable.faxian);
wobutton.setImageResource(R.drawable.wo);
}
}
2.2.3adpter适配器
package com.example.homework;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.mViewHolder> {
private Context mContext;
public List<Message> messageList;
static class mViewHolder extends RecyclerView.ViewHolder{
TextView TitleName;
TextView TitleContent;
ImageView TitleImg;
public mViewHolder(View itemView){
super(itemView);
TitleName=itemView.findViewById(R.id.textView);
TitleContent=itemView.findViewById(R.id.textView6);
TitleImg=itemView.findViewById(R.id.imageView2);
}
}
public MessageAdapter(Context context,List<Message> list ){
messageList=list;
this.mContext=context;
}
@NonNull
@Override
public mViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int ViewType){
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list,parent,false);
mViewHolder myViewHolder=new mViewHolder(view);
return myViewHolder;
}
@Override
public void onBindViewHolder(mViewHolder holder, int position){
Message messages=messageList.get(position);
holder.TitleName.setText(messages.name);
holder.TitleContent.setText(messages.content);
holder.TitleImg.setImageResource(messages.getImgId());
holder.TitleName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent= new Intent();
intent.setClass(mContext, activity1.class);
//intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);
}
});
}
@Override
public int getItemCount(){
return messageList.size();
}
}
2.3生命周期
activity1
package com.example.homework;
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 activity1 extends AppCompatActivity {
private static final String TAG = "Activity1";
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.d("yjj",TAG+" create");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_1);
TextView backText=findViewById(R.id.backText);
Button buttonCollect=findViewById(R.id.buttonCollect);
ActivityResultLauncher launcher=registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
if(result.getResultCode()==RESULT_OK){
Log.d("yjj",TAG+"接受回传值:data="+
result.getData().getStringExtra("data_return"));
backText.setText("Collected Times:"+result.getData()
.getStringExtra("data_return"));
}
}
});
buttonCollect.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
Intent intent=new Intent(activity1.this,activity2.class);
launcher.launch(intent);
}
});
}
// protected void onStart() {
// super.onStart();
// Log.d("yjj",TAG+" start");
// }
@Override
protected void onDestroy() {
super.onDestroy();
Log.d("yjj",TAG+" destroyed");
}
}
activity2
package com.example.homework;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class activity2 extends AppCompatActivity {
public static int i=0;
private static final String TAG = "Activity2";
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.d("yjj",TAG+" create");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
Button buttonQuit=findViewById(R.id.buttonQuit);
i++;
String s=Integer.toString(i);
buttonQuit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d("yjj",TAG+"回传值"+s);
Intent intent=new Intent();
intent.putExtra("data_return",s);
setResult(RESULT_OK,intent);
finish();
}
});
}
// @Override
// protected void onStart() {
// super.onStart();
// Log.d("yjj",TAG+" start");
// }
@Override
protected void onDestroy() {
super.onDestroy();
Log.d("yjj",TAG+" destroyed");
}
}
三实验结果截图
四 仓库地址