1.使用log展示生命周期的状态变化
代码如图
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Log.d("lxy","cratee");
button=findViewById(R.id.button5);
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View arg0) {
Intent intent=new Intent(MainActivity2.this, ItemActivity.class);
startActivity(intent);
}});
}
@Override
protected void onStart() {
super.onStart();
Log.i("Activity02","onStart()");
}
@Override
protected void onRestart() {
super.onRestart();
Log.i("Activity02","onRestart()");
}
@Override
protected void onResume() {
super.onResume();
Log.i("Activity02","onResume()");
}
@Override
protected void onPause() {
super.onPause();
Log.i("Activity02","onPause()");
}
@Override
protected void onStop() {
super.onStop();
Log.i("Activity02","onStop()");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.i("Activity02","onDestroy()");
}
当点击button时,开始运行activity_main2
用系统的返回按键
点击页面返回按键
点击home键
2.Activity跳转
功能体现:新建一个新的activity1,recycleview的某一项点击后跳转到这个新的activity1
参考https://www.jianshu.com/p/c4cfe38a91ed
这里采用的方法是在Adapter
里面直接对控件做点击事件
代码如下:
@Override
public void onBindViewHolder(myviewholder myviewholder, int j) {
myviewholder.textView.setText(list.get(j));
myviewholder.textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent(context,ItemActivity.class);
context.startActivity(intent);
}
});
}
3.实现activity for result功能
具体描述:新建一个新的activity2,在activity1上添加按钮可收到activity2的回传值。如:新闻详情页面中点击收藏按钮可显示当前的收藏数量为N。
这里采用的是StartActivityForResult
。
StartActivityForResult:
使用Intent在activity(fragment)间通信,返回ActivityResult。
代码如下
ItemActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item);
textview=findViewById(R.id.itemtextview2);
button=findViewById(R.id.button3);
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 = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>(){
@Override
public void onActivityResult(ActivityResult result) {
if (result.getResultCode() == 888){
Log.d("lxy","onActivityResultLauncher...");
BreakIterator textView;
textview.setText(result.getData().getStringExtra("data"));
}
}
});
ResultActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
Intent intent=getIntent();
intent.putExtra("data","现在是"+ Calendar.getInstance().getTime());
setResult(888,intent);
finish();
}
结果截图
点击某一项,跳转到如图所示界面
点击按钮,得到返回信息
4.仓库
liuxinyi-monday/windows program - Gitee.com