如何使用界面将活动回调回片段 [英] How to get callback from activity back to fragment using interface

查看:161
本文介绍了如何使用界面将活动回调回片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个片段,我有一个按钮可以从图库中选择图片。画廊成功开放,但是当我选择图片时,我没有从活动中得到结果。所以我考虑使用回调(接口)。但是,我确实知道如何。



您可以给我一些建议吗?

界面

  public interface CallbackListener {
void onPhotoTake(String url);
}

片段点击

  @OnClick(R.id.addPhoto)void photo(){
if(isStoragePermissionGranted()){
Intent i =新的意图(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
context.startActivityForResult(i,RESULT_LOAD_IMAGE);
}
}

活动

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




  • 这里我想发送结果返回片段使用接口
  • 如果您想要发回数据,请使用

解决方案

对于片段类可以有多种方式来做到这一点:

第一


  1. 在片段类中创建公共方法

  2. 使用<活动类的 onActivityResult()







第二


  1. 在活动中创建界面并在片段中实现该接口。然后在接口的帮助下,将你的数据发送到片段
    例如

    $ b $ p Activity class


      DataReceivedListener侦听器; 

    public void setDataReceivedListener(DataReceivedListener listener){
    this.listener = listener;
    }

    public interface DataReceivedListener {
    void onReceived(int requestCode,int resultCode,Intent data)
    }

    片段类

      class fragment extends Fragments实现yourActivityClassName。 DataReceivedListener {
    @Override
    onViewCreated(...){




    $ b((yourActivityName)getActivity())。setDataReceivedListener(this);


    $ / code>

    在Acitivity类中

      @Override 
    protected void onActivityResult(int requestCode,int resultCode,Intent data){
    super.onActivityResult(requestCode,resultCode,data) ;
    if(listener!= null){
    listener.onActivityResult(requestCode,resultCode,data);
    }
    }


    I have a fragment, where I have a button to choose an image from a gallery. The gallery is open successfully, but when I choose the image, I do not get the result from activity. So I consider to use a callback (interface). However, I do know how.

    Could you suggest me something please?

    interface

    public interface CallbackListener {
        void onPhotoTake(String url);
    }
    

    in fragment click

    @OnClick(R.id.addPhoto) void photo() {
            if (isStoragePermissionGranted()) {
                Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                context.startActivityForResult(i, RESULT_LOAD_IMAGE);
            }
    }
    

    activity

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

    • here I would like to send result back to fragment using interface

    解决方案

    If you want to send data back to fragment class there can be a number of ways to do that:

    First

    1. Create a public method in fragment class

    2. Call that method using the fragment object in activity class's onActivityResult()


    Second

    1. Create an interface in activity and implement that interface in fragment. Then with the help of interface, send your data to fragment e.g.

    Activity class

    DataReceivedListener listener;
    
    public void setDataReceivedListener(DataReceivedListener listener) {
        this.listener = listener;
    }
    
    public interface DataReceivedListener {
        void onReceived(int requestCode, int resultCode, Intent data)
    }
    

    Fragment Class

    class fragment extends Fragments implements yourActivityClassName.DataReceivedListener {
        @Override
        void onReceived(int requestCode, int resultCode, Intent data) {
    
        }
    
        onViewCreated(...) {
            ((yourActivityName) getActivity()).setDataReceivedListener(this);
        }
    }
    

    in Acitivity class

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (listener != null) {
            listener.onActivityResult(requestCode, resultCode, data);
        }
    }
    

    这篇关于如何使用界面将活动回调回片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆