onActivityResult不适用于片段

时间:2015-06-29 14:04:48

标签: android

我的变化 enter image description here

fragclient>活动> fragsearchclient(fragsearchclient为fragclient返回成功)

fragproduct>活动> fragsearchprodutc(fragsearch produtc返回falied,不调用fragproduct的onActivityResult)

帮助?

例如

public class MainActivityFragment extends Fragment {

    public MainActivityFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_main, container, false);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        startActivity(new Intent(getActivity(), MainActivity2Activity.class));
    }

}

public class MainActivity2ActivityFragment extends Fragment {

    public MainActivity2ActivityFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_main_activity2, container, false);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        startActivityForResult(new Intent(getActivity(), MainActivity22Activity.class), Activity.RESULT_OK);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }
}

public class MainActivity22ActivityFragment extends Fragment {

    public MainActivity22ActivityFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_main_activity22, container, false);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getActivity().setIntent(new Intent());
        getActivity().setResult(Activity.RESULT_OK);
        getActivity().finish();
    }
}

onActivityResult无法处理片段

1 个答案:

答案 0 :(得分:0)

我的应用程序遇到了同样的问题!当我调用'Camera Activity'时,我的应用程序包含1个活动和不同的片段我需要捕获'OnActivityResult'事件。然后我可以在我的活动中使用'OnActivityResult'来解决这个问题,在我的示例中,我的相机活动返回一个字符串,并且此字符串被声明为公共静态字符串,因为我的片段可以访问此值。

你永远无法在片段中捕获'OnActivityResult'事件,因为此事件属于'ActivityClass',然后你需要捕获调用片段的活动类中的事件!!

public class  MainActivity extends Activity {
    public static String ActivityResultValue;

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data)   {
        super.onActivityResult(requestCode, resultCode, data);
        switch(requestCode) {
            //Camera Activity
            case (0) : {
                if (resultCode == Activity.RESULT_OK) {
                    ActivityResultValue = data.getStringExtra("photoName");
                }
                break;
            }
            case (1) :{
                //Other OnActivityResults...
                break;
            }
        }
    }
}

告诉我如果我帮助你并做好编程!!