从片段通话startActivityForResult不叫onActivityResult不叫、片段、startActivityForResult、onActivityResult

2023-09-12 04:59:37 作者:疯疯癫癫~要不要

我有一个 DialogActivity 这是从片段作秀自定义对话称为有两个图像按钮。

I have a DialogActivity which is called from a Fragment for show a custom Dialog with two image buttons.

DialogActivity.onCreate

final Dialog dialog = new Dialog(this, R.style.DialogTheme);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog_pause); 
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog.show();

DialogActivity.onClick

        @Override
        public void onClick(View v) {
            Log.d(LOGTAG, "onClick CONTINUE");

            Intent resultData = new Intent();
            resultData.putExtra("TEST", "return data");
            setResult(666, resultData);
            dialog.cancel();
        }

在片段调用 startActivityForResult

Intent dialogActivityIntent = new Intent(getActivity(), DialogActivity.class);
startActivityForResult(dialogActivityIntent, 999);

活动片段调用 startActivityForResult

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

    super.onActivityResult(requestCode, resultCode, data);
}

当我按一下按钮我只得到了对话框取消,并显示在后台活动(片段)。

When I click the button I only get the dialog cancel and shows the background activity (fragment).

没有任何调用 onActivityResult onResume ,...在片段活动包含片段

There isn't any call to onActivityResult, onResume, ... in the Fragment or the Activity contains the Fragment.

要落实 onActivityResult 在两个片段活动包含我的片段

To implement onActivityResult in both, Fragment and Activity that contains my Fragment.

我将在每一个的活动属性 noHistory = TRUE 我有。

I set the attribute noHistory=true in every Activity I have.

如果我这样做完成()的onClick 活动/片段调用 DialogActivity 太封闭,并且应用程序返回到活动

If I do finish() in onClick the Activity/Fragment that calls DialogActivity is closed too, and the application return to the before Activity.

这可能是问题,我不叫完成() ...但如果​​我叫完成(),退出到另一个活动,而不是活动调用 startActivityForResult

This may be the problem, I DON'T call finish() ... but if I call finish(), it exits to another Activity, not the Activity that calls startActivityForResult.

startActivityForResult()不打电话onActivityResult(INT申请code,INT结果code,意图数据)?

Cannot到达触发onActivityResult()的Andr​​oid?

startActivityForResult似乎不叫onActivityResult

onActivityResult从来没有所谓的

Android的onActivityResult从来没有所谓的

onActivityResult()在传唤时没有活性的片段

我希望一切都清楚地解释^^。

I hope everything is clearly explained ^^.

在此先感谢。

推荐答案

活动具有属性 noHistory = TRUE 将永远不会有自己的 onActivityResult( )推出一个新的活动通过 startActivityForResult时,称为()。由于documentation提到,当 noHistory 属性设置为,然后完成()是呼吁活动

Activities that have the attribute noHistory=true will never have their onActivityResult() called when launching a new Activity via startActivityForResult(). As the documentation mentions, when the noHistory attribute is set to true, then finish() is called on the Activity when the user navigates away from the Activity.

所以,当 startActivityForResult()被调用时,活动是从,导致其完成()来调用,使之永远不会收到呼叫 onActivityResult()。如果您删除从活动这是调用 startActivityForResult(),然后调用完成() DialogActivity 的onClick( ),那么你还是应该看到活动启动它,以及接收呼叫 onActivityResult()

So, when startActivityForResult() is called, the Activity is navigated away from, causing its finish() to be called and making it never receive a call to onActivityResult(). If you remove the noHistory=true attribute from the Activity that's calling startActivityForResult(), then call finish() in your DialogActivity's onClick(), then you should still see the Activity that launched it, as well as receive a call to onActivityResult().