调用一个片段的onActivityResult,该片段在另一个片段的视图寻呼机索引上

时间:2019-03-15 09:40:53

标签: android android-fragments onactivityresult

我有一个MainAcitivity,在它上面有一个片段A,其中包含带有两个片段B和C的视图寻呼机。....Frgamnet B在片段A中位于viewpager的0索引处,并包含要上传的配置文件部分来自画廊或相机的个人资料图片。当选择图片时,将调用主活动的onActivity结果,然后将其重定向到viewpager frgamentA的实例,但是我的问题是如何调用片段B的onActivityResult,其中要显示主图片在imageview中设置? 主要活性->碎片A->片段B

期望得到积极的答复。

FRAGMENTB.java ......

private void galleryIntent() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);//
    check="file";
    startActivityForResult(Intent.createChooser(intent, "Select File"), SELECT_FILE);
}

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
      if (check.equalsIgnoreCase("file"))
            onSelectFromGalleryResult(data);
        else if (check.equalsIgnoreCase("camera"))
            onCaptureImageResult(data);
    }
}

MAIN ACTIVITY.java。

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    Fragment f = getSupportFragmentManager().findFragmentById(R.id.frame);
    if (f instanceof FragmentA) {    //fragement A  is viewpager fragment
        f.onActivityResult(requestCode, resultCode, intent);
    }
}

FRAGMENTA.java ............

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    getTargetFragment().onActivityResult(requestCode, resultCode, intent);
}

HI需要调用FragmentB的onActivityResult

4 个答案:

答案 0 :(得分:0)

如果您要分段调用startActivityForResult,则将首先使用getActivity()

private void galleryIntent() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);//
    check="file";
    getActivity().startActivityForResult(Intent.createChooser(intent, "Select File"), SELECT_FILE);

}

答案 1 :(得分:0)

您需要使用请求代码致电void setBob(const Bob& bob) { m_bob.emplace(bob); } 我已经创建了一个可以正常工作的示例。

活动文件

startActivityForResult

片段A

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;

public class ViewPagerActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_viewpager);


        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.frameContainer, new FragmentA(), "ft");
        transaction.addToBackStack(null);
        transaction.commit();

    }
}

FragmentB

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.Objects;

public class FragmentA extends Fragment {


    ViewPager viewPager;
    ViewPagerAdapter viewPagerAdapter;

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


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        viewPager = view.findViewById(R.id.viewPager);
        viewPagerAdapter = new ViewPagerAdapter(Objects.requireNonNull(getActivity()).getSupportFragmentManager());
        viewPager.setAdapter(viewPagerAdapter);

    }

    public class ViewPagerAdapter extends FragmentStatePagerAdapter {
        private int NUM_ITEMS = 2;

        public ViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {
            switch (i) {
                case 0: // Fragment # 0 - This will show FirstFragment
                    return new FragmentB();
                case 1: // Fragment # 0 - This will show FirstFragment different title
                    return new FragmentC();
                default:
                    return null;
            }
        }

        @Override
        public int getCount() {
            return 2;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "Frag B";
                case 1:
                    return "Frag C";
            }
            return "Page " + position;
        }
    }
}

这是 FragmentC ,您在其中调用public class FragmentB extends Fragment { @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.frag_b, container, false); } }

startActivityForResult

答案 2 :(得分:0)

您必须从必须处理activityResult的组件中调用startActivityForResult()

例如:

如果要在Activity myAwesomeActivity中处理结果:

myAwesomeActivity.startActivityForResult();

如果要在Fragment myAwesomeFragment中处理结果

myAwesomeFragment.startActivityForResult();

答案 3 :(得分:0)

为此,最好致电

getParentFragment().startActivityForResult(), 

,然后在该父片段中,可以实现

 onActivityResult()

,然后用这种方法将结果传递给子fragmen,

  childFragment.getParentFragment().onActivityResult(requestCode, resultCode, data)