can we call startActivityForResult from adapter?How to get the response?

后端 未结 5 1692
说谎
说谎 2020-12-14 06:18

is it possible to have method startActivtyForResult within an adapter?Then how to get the response? Where to execute the call back function?

5条回答
  •  醉酒成梦
    2020-12-14 07:00

    I used a simpler method. create a public function in you activity/fragment which will call activity for result

    public void startActivityFromAdapter(String Arguments){
        //todo: add steps you would like to compute
        startActivityForResult(Intent, REQ_CODE);
    }
    

    When creating the adapter, pass the current activity/fragment as an argument. I will use activity for example

    public MyAdaper(Activity activity, ArrayList list){
        this.activity = activity;
        this.list = list;
    }
    

    call the public function from viewholder by casting the activity to the Activity Class

     @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        
        holder.applyBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ((ActivityName) activity).startActivityFromAdapter(list.get(position).code);
            }
        });
    }
    

    and use onActivityResult in your calling activity/fragment

提交回复
热议问题