-1
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        v = inflater.inflate(R.layout.fragment_fragment_home, container, false);


        FloatingActionButton floatingActionButton=(FloatingActionButton) v.findViewById(R.id.picker);
        floatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (ContextCompat.checkSelfPermission(getActivity() , Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED) {
                    new MultiContactPicker.Builder(getActivity()) //Activity/fragment context
                            .hideScrollbar(false) //Optional - default: false
                            .showTrack(true) //Optional - default: true
                            .searchIconColor(Color.WHITE) //Optional - default: White
                            .setChoiceMode(MultiContactPicker.CHOICE_MODE_MULTIPLE) //Optional - default: CHOICE_MODE_MULTIPLE
                            .handleColor(ContextCompat.getColor(getActivity() , R.color.colorPrimary)) //Optional - default: Azure Blue
                            .bubbleColor(ContextCompat.getColor(getActivity() , R.color.colorPrimary)) //Optional - default: Azure Blue
                            .bubbleTextColor(Color.WHITE) //Optional - default: White
                            .showPickerForResult(CONTACT_PICKER_REQUEST);
                }else{
                    Toast.makeText(getActivity(), "Remember to go into settings and enable the contacts permission.", Toast.LENGTH_LONG).show();
                }
            }
        });
        recyclerView=(RecyclerView) v.findViewById(R.id.recyclerView);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        listItems = new ArrayList<>();

        return v;
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == CONTACT_PICKER_REQUEST){
            if(resultCode == RESULT_OK) {
                List<ContactResult> results = MultiContactPicker.obtainResult(data);
                for (int i=0;i<results.size();i++){
                    ListItem listItem = new ListItem(
                            "Name:  "+results.get(i).getDisplayName(),
                            "Contact No: "+results.get(i).getPhoneNumbers()
                    );
                    listItems.add(listItem);
                }

                adapter = new MyRecycleAdapter(listItems,this.getActivity());
                recyclerView.setAdapter(adapter);

                Toast.makeText(getActivity(), "MyTag"+results.get(0).getDisplayName(), Toast.LENGTH_SHORT).show();
                Log.d("MyTag", results.get(0).getDisplayName());
            } else if(resultCode == RESULT_CANCELED){
                System.out.println("User closed the picker without selecting items.");
            }
        }
    }

How can I use this onActivityResult in fragment? Whenever I run this code at last "adapter is not connected" if this method is used as protected it works but in fragment does not support protected can anyone help me, please.

Faysal Ahmed
  • 7,501
  • 5
  • 28
  • 50

2 Answers2

1

Not sure which MultiContactPicker you are using, but if it's this one: https://github.com/broakenmedia/MultiContactPicker, it looks like you can/should pass the fragment when you instantiate the Builder. So change this line:

new MultiContactPicker.Builder(getActivity())

to:

new MultiContactPicker.Builder(this)
mikejonesguy
  • 9,779
  • 2
  • 35
  • 49
0

Do not call super in fragment's onActivityResult, remove this line:

super.onActivityResult(requestCode, resultCode, data);

and override onActivityResult in your Activity and call super there:

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

EDIT:

After looking at your project I figured out the problem, it's a combination of the two answers you got. First in your MainActivity add onActivityResult, so your MainActivity will look like this:

public class MainActivity extends AppCompatActivity {

    private TabLayout tabLayout;
    private ViewPager viewPager;
    private viewPagerAdapter viewPagerAdapter;



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

        tabLayout = (TabLayout) findViewById(R.id.tabLayout);
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        viewPagerAdapter = new viewPagerAdapter(getSupportFragmentManager());

        viewPager.setAdapter(viewPagerAdapter);
        tabLayout.setupWithViewPager(viewPager);

    }

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

Then in your FragmentHome in the floatingActionButton onClick method change this line:

new MultiContactPicker.Builder(getActivity())

to this line:

new MultiContactPicker.Builder(FragmentHome.this)

And

  public class ViewPagerAdapter extends FragmentPagerAdapter {
      public ViewPagerAdapter(FragmentManager fragmentManager) {
          super(fragmentManager);
      }
  
      @Override
      public Fragment getItem(int position) {
          if(position == 0) return new FragmentHome();
          if(position == 1) return new FragmentRent();

          throw new IllegalStateException("Unexpected position " + position);
      }
  
      @Override
      public int getCount() {
          return 2;
      }
  
      @Override
      public CharSequence getPageTitle(int position) {
          if(position == 0) return "Home";
          if(position == 1) return "Rent";
        
          throw new IllegalStateException("Unexpected position " + position);
      }
  }

And it will work, I tested it, the contact appears after that.

EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
Suleyman
  • 2,765
  • 2
  • 18
  • 31