gpt4 book ai didi

android - ResultCode 和 RequestCode 不工作

转载 作者:行者123 更新时间:2023-11-30 01:09:55 25 4
gpt4 key购买 nike

我使用请求代码从 MainActivity 调用到 InfomationActivity。但是,当返回 MainActivity 时,它处于非 Activity 状态。这里有什么问题?

MainActivity 中,使用请求代码调用 InfomationActivity:

Intent intent = new Intent(MainActivity.this, InfomationActivity.class);
startActivityForResult(intent, 100);

InfomationActivity中,返回一个resultcode:

        if(btnAlarmInfo.getVisibility() == View.VISIBLE){
//run
Log.d("abc", note.getTitle() + "/" + note.getNote() + "/" + note.getDateTime() + "/" + note.getColorBackground());
Log.d("abc", Integer.toString(images.size()));
Intent intent = getIntent();
intent.putExtra("title", note.getTitle());
intent.putExtra("note", note.getNote());
intent.putExtra("time", note.getDateTime());
intent.putExtra("color", note.getColorBackground());
intent.putParcelableArrayListExtra("image", images);
setResult(3, intent);
finish();
}else{
//run
Intent intent = getIntent();
intent.putExtra("title", note.getTitle());
intent.putExtra("note", note.getNote());
intent.putExtra("time", note.getDateTime());
intent.putExtra("color", note.getColorBackground());
intent.putExtra("day", note.getDayAlarm());
intent.putExtra("hour", note.getHourAlarm());
intent.putParcelableArrayListExtra("image", images);
setResult(4, intent);
finish();
}

MainActivity 返回时:

if(requestCode == 100){
if(resultCode == 3){
//not run ????????
Log.d("abc", "it's me");
String title = data.getExtras().getString("title");
String note = data.getExtras().getString("note");
String time = data.getExtras().getString("time");
String color = data.getExtras().getString("color");
ArrayList<Image> image = data.getParcelableArrayListExtra("image");
Log.d("abc", Integer.toString(image.size()));
ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>();
for(int i = 0; i < image.size(); i++){
bitmaps.add(image.get(i).getImage());
}
Note note1 = new Note(title, note, false, time, color, "", "", bitmaps);
this.addNote(note1);
}else if(resultCode == 4){
//run
String title = data.getExtras().getString("title");
String note = data.getExtras().getString("note");
String time = data.getExtras().getString("time");
String color = data.getExtras().getString("color");
String day = data.getExtras().getString("day");
String hour = data.getExtras().getString("hour");
ArrayList<Image> image = data.getParcelableArrayListExtra("image");
ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>();
for(int i = 0; i < image.size(); i++){
bitmaps.add(image.get(i).getImage());
}
Note note1 = new Note(title, note, true, time, color, day, hour, bitmaps);
this.addNote(note1);
}
}

在 logcat 中,当 resultcode = 3 时,我看到它不运行。为什么当 resultcode = 3 时,它不运行?

最佳答案

应该在启动的 Activity 中使用预定义值 RESULT_OKRESULT_CANCELLEDRESULT_FIRST_USER 之一设置结果代码.

像 3 和 4 这样的设置值在这里不一定有定义的行为;如果它们对您有某种意义(并且不仅仅是随机的),则将它们作为 Intent 的另一个额外内容传回。使用 RESULT_OK 代替它们。

此外,对于您用来传回结果的 Intent,我建议使用 new Intent() 创建一个新 Intent 而不是重复使用用于启动 Activity 的 Intent(即getIntent() 给你什么)。

无论如何,我建议您查看this section of the docs ,它处理开始 Activity 和获得结果。

关于android - ResultCode 和 RequestCode 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38517342/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com