我想把这个添加到另一个关于resultCode == 0
和requestCode == 0
的问题列表中。
以下是概要:NoteActivity
使用NoteSettingsActivity
调用startActivityForResult()
。
我已经搜索过网络,当我按下后退按钮时,super.onBackPressed()
。经过进一步的研究,似乎只要按下back按钮,它就会返回这段代码,但是在把resultCode == 0
调用和简单的super.onBackPressed()
应用程序都搞糟之后,应用程序的finish()
的onActivityResult()
仍然等于0。与resultCode
相同。
另外,我试着操纵清单文件,我做了这么多的修改只是为了完成这项工作,但什么都不适合我。
这是片段。请注意,我已恢复到上一次提交,因此丢失了最近的修改,但请查看我编写的代码,然后我才注意到requestCode
始终等于0(resultCode
)
@Override
public void onNoteSettingsActivityCalled(Note note)
{
Intent intent = new Intent(this, NoteSettingsActivity.class);
intent.putExtra(NoteExtrasKey.EXTRA_NOTE_ID, note.getNoteID());
startActivityForResult(intent, NoteRequest.REQUEST_UPDATE_SETTINGS);
}
当检测到活动时,请按以下键:
@Override
public void onBackPressed()
{
Log.i(NoteApplication.TAG, "NoteSettingsActivity.onBackPressed() has been called.");
Intent intent = new Intent();
intent.putExtra(NoteExtrasKey.EXTRA_NOTE_REMINDENABLED , mRemindEnabled);
intent.putExtra(NoteExtrasKey.EXTRA_NOTE_REMINDEVERY , mDaysSelected);
intent.putExtra(NoteExtrasKey.EXTRA_NOTE_REMINDON , String.valueOf(mRemindDateTime));
intent.putExtra(NoteExtrasKey.EXTRA_NOTE_ID , mTargetNoteID);
if(getParent() != null)
getParent().setResult(Activity.RESULT_OK, intent);
else
setResult(Activity.RESULT_OK, intent);
super.onBackPressed();
}
下面是noteactivity如何接收结果调用。
@Override
public void onActivityResult(int result, int request, Intent intent)
{
super.onActivityResult(result, request, intent);
Log.i(NoteApplication.TAG, "NoteActivity.onActivityResult() has been called.");
Log.i(NoteApplication.TAG, "NoteActivity.onActivityResult() result = " + result + " request = " + request);
if(result == Activity.RESULT_CANCELED)
return;
switch(request)
{
case NoteRequest.REQUEST_UPDATE_SETTINGS:
if(intent == null) return;
int noteID = intent.getIntExtra(NoteExtrasKey.EXTRA_NOTE_ID, -1);
String remindOnString = intent.getStringExtra(NoteExtrasKey.EXTRA_NOTE_REMINDON);
if(remindOnString != null && !remindOnString.equals(""))
mRemindDateTime = Timestamp.valueOf(remindOnString);
mHasSettingsEnabled = true;
mRemindEnabled = intent.getBooleanExtra(NoteExtrasKey.EXTRA_NOTE_REMINDENABLED, false);
mSelectedDays = intent.getIntegerArrayListExtra(NoteExtrasKey.EXTRA_NOTE_REMINDEVERY);
if(noteID < 0)
{
Note note = mNoteDatabaseHelper.getNote(noteID);
note.setRemindEnabled(mRemindEnabled);
note.remindEvery(mSelectedDays);
note.remindOn(mRemindDateTime);
onNoteItemUpdated(note);
}
Log.i(NoteApplication.TAG, "NoteActivity.onActivityResult() NoteRequest.REQUEST_UPDATE_SETTINGS called.");
break;
default:
Log.i(NoteApplication.TAG, "NoteActivity.onActivityResult() : unknown request code = " + request);
break;
}
}
ACTIVITY_CANCELED
等于0并且resultCode
Requals-1。我已经检查了传递给它的意图,它不是空的。以下是与这个问题非常相关的问题。他们都不起作用:
Android onActivityResult is always 0
How do I handle the back button when startActivityForResult isactive?
setResult does not work when BACK button pressed
Android: startActivityForResult always gets a resultCode of 0?
Android Calendar: onActivityResult's resultCode is always 0
onActivityResult always returns 0
onActivityResult() is being called at an unexpected time
Android - startActivityForResult immediately triggeringonActivityResult
onActivityResult() called prematurely
Activity result is always 0
Android: Gallery intent returning resultCode == RESULT_CANCELED
Android on activity result always return 0 and null intent
我在我的项目中失去了很多重要的工作时间,只是弄清楚是什么使resultcode和requestcode的值失去了我一路上发送的值。
任何帮助和指导将不胜感激。非常感谢你!
最佳答案
正确的签名是
public void onActivityResult(int requestCode, int resultCode, Intent data) {
你的要求和结果弄混了。
关于android - resultCode始终为0,请求始终为-1。 Activity.onActivityResult();,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29972183/