Android 返回上一个界面刷新数据
程序员文章站
2022-04-02 21:17:10
startActivityForResult()MainActivity不带参@Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { if (requestCode==1){ //刷新操作 } }带参数@Overrideprotected void onActivit...
startActivityForResult()
MainActivity
不带参
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if (requestCode==1){
//刷新操作
}
}
带参数
@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data){
switch(requestCode){
case 1:
if(resultCode==RESULT_OK){
String returnData = data.getStringExtra("data_return");
Log.d("FirstActivity,"returnData");
}
break;
default:
}
}
MainActivity
不带参
Intent intent = new Intent(activity, MainActivity.class);
activity.startActivityForResult(intent,1);
activity.finish();
带参
Intent intent = new Intent();
intent.putExtra("data_return","Hello SecondActivity");
setResult(RESULT_OK,intent);
finish();
onResume()生命周期
重写onResume()
@Override
protected void onResume() {
super.onResume();
//重新获取数据的逻辑,此处根据自己的要求回去
//显示信息的界面
setContentView(R.layout.first_layout);
initView();
}
onResume是在启动activity启动之后才能执行的,也就是恢复执行。程序正常启动:onCreate()->onStart()->onResume();
正常退出:onPause()->onStop()->onDestory()
一个Activity启动另一个Activity: onPause()->onStop(), 再返回:onRestart()->onStart()->onResume()
程序按back 退出: onPause()->onStop()->onDestory(),再进入:onCreate()->onStart()->onResume();
程序按home 退出: onPause()->onStop(),再进入:onRestart()->onStart()->onResume();
本文地址:https://blog.csdn.net/weixin_44177244/article/details/108861732
下一篇: NDK开发趟坑之旅