首页 > 其他分享 >onBackPressed 设置返回值,onActivityResult resultcode一直是0

onBackPressed 设置返回值,onActivityResult resultcode一直是0

时间:2023-09-12 10:13:27浏览次数:35  
标签:onBackPressed requestCode Intent intent resultcode onActivityResult resultCode

activity1:

  Intent intent2 =new Intent(this,checkactivity.class);

startActivityForResult(intent2,1000);

activity2:
   @Override
public void onBackPressed() {
super.onBackPressed();

Intent intent = new Intent();

setResult(result,intent);
finish();
}

activity1:
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {


super.onActivityResult(requestCode, resultCode, data);
Log.i("LOG_RES:","Checking.." + requestCode +"," + resultCode);
if (requestCode==1000){



int curProgress = progressBar.getProgress();

// if (curProgress==progressBar.getMax()){
//
// progressBar.setProgress(0);
//
// }else {

progressBar.setProgress(curProgress + resultCode*10);

// }

resultcode一直是0



点击返回键,没有正确得到回调!这是为何?因为OnBackPressed() 会默认设置setResult(RESULT_CANCELED);

解决方案:


  @Override
public void onBackPressed() {


Intent intent = new Intent();
// intent.putExtra("returnFirst", "我回来了");
setResult(result,intent);
finish();
super.onBackPressed();


}
 

标签:onBackPressed,requestCode,Intent,intent,resultcode,onActivityResult,resultCode
From: https://www.cnblogs.com/fairylandcom/p/17695259.html

相关文章