37

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

user
  • 1,979
  • 4
  • 18
  • 18
  • create interface and implement on your adapter pass what ever data you want and call adapter.yourmethodname() form onActivityResult.. – Imtiyaz Khalani Nov 16 '13 at 07:32

5 Answers5

75

Yes, it's possible. You need a reference for the Context in the adapter and call the activity:

Intent intent = new Intent(context, TargetActivity.class);
((Activity) context).startActivityForResult(intent, REQUEST_FOR_ACTIVITY_CODE);

Beware that context must be an activity context or this code will fail.

You get the result in the enclosing activity using onActivityResult as usual.

So, for example:

In your adapter:

MyAdapter(Context context) {
    mContext = context;
}

public View getView(int position, View convertView, ViewGroup parent) {
    …
    open.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            …
            Activity origin = (Activity)mContext;
            origin.startActivityForResult(new Intent(mContext, SecondActivity.class), requestCode);
        }   
    });
    …
}

public  void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.d("MyAdapter", "onActivityResult");
}

In your second activity, do as usual with setResult and finish.

In your main activity, capture the result and pass to the adapter callback:

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

Yes.You can call startactivityforresult() from adapter.

There are two case- 1.Calling adapter from activity and need onActivityResult in activity. 2.Calling adapter from Fragment and need onActivityResult in fragment.

Case 1:Get OnActivityResult in Activity then pass the reference of activity to adapter constructor

public MyAdapter(Activity pActivity, List<MyBean> pList) {
        mList = pList;
        mActivity = pActivity;       
    }

Now startActivityForResult

Intent intent = new Intent(context, TargetActivity.class);
mActivity.startActivityForResult(intent, REQUEST_FOR_ACTIVITY_CODE);

Case 2: Get OnActivityResult in Fragment then pass the reference of fragment to adapter constructor

 public MyGamesAdapter(Fragment pContext, List<MyBean> pList,) {
        mList = pList;
        mMyFragment =pContext;
    }

 Intent intent = new Intent(context, TargetActivity.class);
    mMyFragment.startActivityForResult(intent, REQUEST_FOR_ACTIVITY_CODE);

Now in activity or fragment override OnActivityResult and get result.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    mAdapter.onActivityResult(requestCode, resultCode, data);
}
sanjaymith
  • 216
  • 3
  • 7
1

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<String> 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

Edit:

I would not recommend using above method as it limits the adapter to be used in only one activity. Better you must use an interface for the same

create an interface and a onClickFunction

public interface AdapterInteface{
    void onBtClick(int Position)
}

now when you create the adapter, accept this interface as an argument and call the function when button is clicked

public MyAdaper(Activity activity, ArrayList<String> list, AdapterInterface adapterInterface){
    this.activity = activity;
    this.list = list;
    this.adapterInterface = adapterInterface
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    
    holder.applyBt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            adapterInterface.onBtClicked(position)
        }
    });
}

Implement this interface on your activity/fragment and override the function onBtClicked on your activity/fragment to startActivityForResult

mohit48
  • 712
  • 7
  • 9
0

write a functon in activity class like this

public void startCommentActivity(Intent i){
    startActivityForResult(i, 100);
}

call it in adapter class

mActivity.startCommentActivity(intent);
Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107
saly
  • 19
  • 1
0

The androidX Activity implementation has a new set of APIs to get an activity result anywhere you wish.

This functionality replaces the old startActivityForResult() / onActivityResult() and onRequestPermissionsResult(), which have been deprecated in the ComponentActivity.

Example starting a second activity and expecting a result from it:

final Intent intent = new Intent(context, SecondActivity.class);
final ActivityResultContracts.StartActivityForResult contract = new ActivityResultContracts.StartActivityForResult();

activity.registerForActivityResult(contract, result ->
    YourClass.manageTheResult(result))
    .launch(intent);

Example requesting user permissions:

final String[] permissions = {Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO};

final ActivityResultContracts.RequestMultiplePermissions contract = new ActivityResultContracts.RequestMultiplePermissions();

activity.registerForActivityResult(contract, result -> {
    YourClass.manageTheResult(permissions, result);
}).launch(permissions);

Example managing a "sender intent":

final IntentSenderRequest request = new IntentSenderRequest.Builder(yourIntentSender)
    .build();

final ActivityResultContracts.StartIntentSenderForResult contract = new ActivityResultContracts.StartIntentSenderForResult();

activity.registerForActivityResult(contract, result ->
    YourClasss.manageTheResult(result))
    .launch(request);
PerracoLabs
  • 16,449
  • 15
  • 74
  • 127
  • While we do have newer APIs now, this doesn't answer the question of doing this from an adapter. – Bink Apr 30 '21 at 21:27