Work requirements:
1. Please realize the understanding of the Activity life cycle according to the course content, and use the log to display the state changes of the life cycle;
2. According to the content of the blog: https://www.jianshu.com/p/c4cfe38a91ed ("click" in the advanced chapter), the single-click function of the list item is added on the basis of the previous homework. The specific requirements are: Create a new activity1, click on an item of recycleview to jump to this new activity1. For example: clicking on the news list will jump to the news details page;
3. To implement the latest activityforresult function, the specific requirements are as follows: Create a new activity2, add a button on activity1 to receive the return value of activity2. For example: Click the favorite button on the news details page to display the current number of favorites as N.
contents of homework:
ResultActivity.java code:
package com.example.work2;
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"+"上课的时间是:"+ Calendar.getInstance().getTime());
setResult(888,intent);
finish();
}
}
activity_item.xml code:
<?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/itemtextView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginEnd="32dp"
android:text="这是返回的信息"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.441"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.772" />
<TextView
android:id="@+id/itemtextView1"
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.529"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.153" />
<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_toBottomOf="@+id/itemtextView1"
app:layout_constraintVertical_bias="0.083" />
</androidx.constraintlayout.widget.ConstraintLayout>
ItemActivity.java code:
package com.example.work2;
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 Button button;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item);
textView=findViewById(R.id.itemtextView2);
button=findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent(ItemActivity.this,ResultActivity.class);
test.launch(intent);
}
});
}
public ActivityResultLauncher test;
{
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"));
}
}
});
}
}
Source Code Warehouse: Work2 Ming/android - Code Cloud - Open Source China (gitee.com)