onActivityResult 在片段中不起作用[重复]

Posted

技术标签:

【中文标题】onActivityResult 在片段中不起作用[重复]【英文标题】:onActivityResult is not working in fragment [duplicate] 【发布时间】:2018-10-30 07:22:37 【问题描述】:
 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.");
            
        
    

如何在片段中使用这个onActivityResult?每当我最后运行此代码时“适配器未连接”,如果此方法用作受保护的,它可以工作,但在片段中不支持受保护的任何人都可以帮助我,拜托。

【问题讨论】:

【参考方案1】:

不确定您使用的是哪个MultiContactPicker,但如果是这个:https://github.com/broakenmedia/MultiContactPicker,看起来您可以/应该在实例化Builder 时传递片段。所以改变这一行:

new MultiContactPicker.Builder(getActivity())

到:

new MultiContactPicker.Builder(this)

【讨论】:

兄弟,我需要在回收站视图上显示数据,但是选择联系人后,当我使用 Fragment 时,它不会在回收站视图上显示联系人。我该怎么办? 如果有人想让我的代码尝试我可以提供,请帮助我,但请帮助我 谢谢你的回答,问题解决了一半:)【参考方案2】:

不要在片段的onActivityResult 中调用super,删除这一行:

super.onActivityResult(requestCode, resultCode, data);

并在您的Activity 中覆盖onActivityResult 并在那里调用super

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

编辑:

查看您的项目后,我发现了问题,这是您得到的两个答案的组合。首先在您的MainActivity 中添加onActivityResult,这样您的MainActivity 将如下所示:

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);
    

然后在您的FragmentHome 中的floatingActionButton onClick 方法中更改此行:

new MultiContactPicker.Builder(getActivity())

到这一行:

new MultiContactPicker.Builder(FragmentHome.this)

  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);
      
  

它会起作用,我测试过,之后出现联系人。

【讨论】:

@Override public void onActivityResult(int requestCode, int resultCode, Intent data) super.onActivityResult(requestCode, resultCode, data); -------------我应该如何在片段中使用它这个代码适用于活动,但不适用于片段-------------------- ---- 兄弟,我需要在回收站视图上显示数据,但是选择联系人后,当我使用 Fragment 时,它不会在回收站视图上显示联系人。我该怎么办? @VectorArora 我在回答中展示了如何使用它。删除此行super.onActivityResult(requestCode, resultCode, data),阅读我的答案,它已解释 no man itz not working can you have the code and try please. @VectorArora 你能确认片段中的onActivityResult 没有被调用吗?输入LogToast

以上是关于onActivityResult 在片段中不起作用[重复]的主要内容,如果未能解决你的问题,请参考以下文章

画廊 OnActivityResult 在 TabActivity 中不起作用

导航组件在 onActivityResult 内导航不起作用

Snackbar 在片段类中不起作用

onRequestPermissionsResult 在片段中不起作用

AlertDialog 在片段中不起作用

片段隐藏在Android中不起作用