DialogInterface.OnClickListener中的startActivityForResult

时间:2014-03-21 14:14:52

标签: android android-intent android-fragments android-camera-intent

我试图用一个选择来实现一个对话框。每个选项都会使用startActivityForResult启动活动,而DialogFragment会使用onActivityResult处理回复。

到目前为止我所拥有的是这样的:

class ChoiceFragment extends DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setItems(sources, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                if (which == 0) {
                    Intent intent = new Intent(SomeActivity.class);
                    startActivityForResult(intent, 0);
                }
                dialog.dismiss();
            }
        });

        return builder.create();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        // This never gets called
    }
}

在我的活动中,我是这样开始片段的:

ChoiceFragment frag = new ChoiceFragment();
frag.show(getFragmentManager(), "choices");

从评论中可以看出,onActivityResult未被调用。

我已经看过几个问题中提到的这个场景,但他们似乎都假设主要活动也在调用onActivityResult,或者该片段被称为getActivity().startActivityForResult(),在我的情况下,这是真的。

2 个答案:

答案 0 :(得分:2)

ChoiceFragment.java:

package com.example.AndroidTest;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

/**
 * @autor Sergey Shustikov
 */
class ChoiceFragment extends DialogFragment {

    private CharSequence[] sources = new CharSequence[]{"Second"};

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setItems(sources, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                if (which == 0) {
                    Intent intent = new Intent(getActivity(),SecondActivity.class);
                    getActivity().startActivityForResult(intent, 0);
                }
                dialog.dismiss();
            }
        });

        return builder.create();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.d("ResultA","This is choice");
        super.onActivityResult(requestCode, resultCode, data);
    }
}

MainActivity.java:

package com.example.AndroidTest;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

public class MyActivity extends Activity {
    private ChoiceFragment frag;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        frag = new ChoiceFragment();
        fragmentTransaction.add(frag,"choices");
        fragmentTransaction.commit();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.d("ResultA", "This is HostActivity");
        super.onActivityResult(requestCode, resultCode, data);
            frag.onActivityResult(requestCode, resultCode, data);
    }
}

SecondActivity.java:

package com.example.AndroidTest;

import android.app.Activity;
import android.os.Bundle;

/**
 * @autor Sergey Shustikov
 */
public class SecondActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
}

答案 1 :(得分:0)

点击here! 这就是答案。