简体   繁体   English

结果代码始终为 0,请求始终为 -1,。 Activity.onActivityResult();

[英]resultCode is always 0 and request is always -1, . Activity.onActivityResult();

I would like to add this to another list of questions about resultCode == 0 and requestCode == 0 .我想将此添加到有关resultCode == 0requestCode == 0另一个问题列表中。

Here is the synopsis: NoteActivity calls NoteSettingsActivity using startActivityForResult() .下面是简介: NoteActivity调用NoteSettingsActivity使用startActivityForResult()

I have searched the web and when I pressed back button when super.onBackPressed() , the resultCode == 0 .我在网上搜索过,当我按下后退按钮时super.onBackPressed()resultCode == 0 Well after further researching, it seems it returns this code whenever the back button is pressed, but after botching that super.onBackPressed() call and just simply finish() the application the onActivityResult() 's resultCode is still equals to 0. It goes the same with requestCode .经过进一步研究,似乎每当按下后退按钮时它都会返回此代码,但是在super.onBackPressed()调用并且只是简单地finish()应用程序之后, onActivityResult()resultCode仍然等于 0。它与requestCode相同。

Also, I tried manipulating the manifest file, I have done so many changes just to get this work but nothings works for me.此外,我尝试操作清单文件,我做了很多更改只是为了完成这项工作,但对我来说没有任何作用。

Here is the snippet.这是片段。 Note that I have reverted back to my previous commit so I have lost my recent modifications, but please take a look on the code I have wrote before I notice that the resultCode is always equals to 0 ( ACTIVITY_CANCELED )请注意,我已恢复到之前的提交,因此我丢失了最近的修改,但是在我注意到resultCode始终等于 0 ( ACTIVITY_CANCELED ) 之前,请查看我编写的代码

@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);
}

Here is when the activity detected back press:这是活动检测到后按的时间:

@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();
}

Here's how NoteActivity received the resulting call.下面是 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;
    }
}

resultCode equals 0 and requestCode requals -1 when I ran this.当我运行它时, resultCode等于 0 并且requestCode等于 -1。 I have checked the intent passed on this and it is not null.我已经检查了传递给它的意图,它不为空。

Here are the questions very related to this question.以下是与这个问题非常相关的问题。 None of them worked:他们都没有工作:

I am losing a lot of important hours working on my project just figuring out what makes the value for resultCode and requestCode lose the value I sent along the way.我在我的项目上失去了很多重要的时间,只是弄清楚是什么让 resultCode 和 requestCode 的值丢失了我沿途发送的值。

Any help and guidance will be appreciated.任何帮助和指导将不胜感激。 Thank you very much!非常感谢!

In my case, I got the resultCode == 0 error because of two reasons.就我而言,由于两个原因,我得到了 resultCode == 0 错误。

Firstly, I copied the method from a fragment instead of from another activity:首先,我从一个片段而不是另一个活动中复制了该方法:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

A good way to prevent this from happening is to @Override your method so that you know you are overriding the default activity onActivityResult method signature.防止这种情况发生的一个好方法是@Override您的方法,以便您知道您正在覆盖 onActivityResult 方法签名的默认活动。

Use the correct signature:使用正确的签名:

public void onActivityResult(int requestCode, int resultCode, Intent data) {

Secondly, I was doing this:其次,我是这样做的:

    super.onBackPressed();
    Intent intent = new Intent();
    intent.putExtra(getString(R.string.like_count), people.size());
    Log.e(TAG, people.size() +" people ");
    setResult(RESULT_OK, intent);
    finish();

The super.onBackPressed() set the resultCode before it was actually set hence it resulted in the 0. super.onBackPressed() 在实际设置之前设置了 resultCode,因此结果为 0。

A simple fix cause it to work properly:一个简单的修复使其正常工作:

    Intent intent = new Intent();
    intent.putExtra(getString(R.string.like_count), people.size());
    Log.e(TAG, people.size() +" people ");
    setResult(RESULT_OK, intent);
    super.onBackPressed();

The call to super.onBackPressed() is the one that will in the end set the result code and send it back to the calling activity.super.onBackPressed()的调用最终会设置结果代码并将其发送回调用活动。 Replace that call with a call to finish() and you should get the result code that you are looking for.将该调用替换为对finish()的调用,您应该会得到您正在寻找的结果代码。

Hope this helps!希望这可以帮助!

The correct signature is正确的签名是

public void onActivityResult(int requestCode, int resultCode, Intent data) {

You have request and result mixed up.你有请求和结果混淆。

Was facing the same issue while doing uninstalling an application from the device.从设备上卸载应用程序时遇到了同样的问题。 It got resolved by putting: intentObject.putExtra(Intent.EXTRA_RETURN_RESULT, true);它通过放置解决:intentObject.putExtra(Intent.EXTRA_RETURN_RESULT, true); before you do startActivityForResult.在你做 startActivityForResult 之前。

For the comers from The Android Big Nerd Ranch Book, it's probably a typo in your code, make sure you're comparing correctly:对于来自The Android Big Nerd Ranch Book 的人来说,这可能是您的代码中的错字,请确保您正确比较:

if (requestCode == REQUEST_CODE_CHEAT) {
if (RESULT_OK == resultCode) {...} 
...
}

Reminder: RESULT_OK is always -1 and it's existence means setResult() was called from the child Activity.提醒: RESULT_OK始终为 -1,它的存在意味着setResult()是从子 Activity 调用的。

I know I am late to the party, but: I had the sam problem, for me it did not work because I was calling onBackPressedDispatcher.onBackPressed() before setting the result.我知道我迟到了,但是:我遇到了 sam 问题,对我来说它不起作用,因为我在设置结果之前调用了onBackPressedDispatcher.onBackPressed()

Like this:像这样:

override fun onBackPressed() {
    onBackPressedDispatcher.onBackPressed() // <---- PROBLEM
    val intent = Intent()
    intent.putExtra("KEY", "VALUE"))
    setResult(RESULT_OK, intent);
    // AND here I tried with this calls without luck. -> requireActivity().finish() or super.onBackPressed()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM