FragmentでActivityのonActivity Resultの結果をどのように受信するか

1735 ワード

FragmentでActivityのonActivity Resultの結果をどのように受信するかは簡単です.
ステップ1:Activityで現在のFragmentオブジェクトを取得し、Fragmentに渡す
 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        try {
            FragmentBase f = (FragmentBase) getSupportFragmentManager().getFragments().get(mViewPager.getCurrentItem());
            f.onActivityResult(requestCode, resultCode, data);
        } catch (Exception e) {
        }
    }

ステップ2:Activityで受信すればよい
  @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE_MAP) {
            //  
            locationResult(data);
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    /** */
    private void locationResult(Intent data) {
        // 
        double latitude = data.getDoubleExtra("latitude", 0);
        // 
        double longitude = data.getDoubleExtra("longitude", 0);

        String locationAddress = data.getStringExtra("address");
        if (locationAddress != null && !locationAddress.equals("")) {
            //toggleMore(more);
            //sendLocationMsg(latitude, longitude, "", locationAddress);
            MyToast.show(getActivity(), "  :" + locationAddress + "  : " + longitude + "," + latitude + "(" + ")", Toast.LENGTH_LONG);
        } else {
            String st = getResources().getString(R.string.unable_to_get_loaction);
            MyToast.show(getActivity(), st, Toast.LENGTH_SHORT);
        }
    }