is it possible to have method onActivityResume
within adapter
& call startActivityForResult
?
Asked
Active
Viewed 3.7k times
40

Pouya Danesh
- 1,557
- 2
- 19
- 36

napster
- 687
- 1
- 8
- 18
-
Yes, are you sure use startActivityForResult in Adapter – ckpatel Aug 09 '12 at 11:59
-
i have a list view that is getting its contents from adapter. I have a button in adapter,on click of button another activity will start & return something – napster Aug 09 '12 at 12:08
-
http://stackoverflow.com/questions/2497205/how-to-return-a-result-startactivityforresult-from-a-tabhost-activity its a complate example same as your quastion .i thing its helpfull..All the Best – ckpatel Aug 09 '12 at 12:10
4 Answers
104
Yes. Just pass the context of the activity to the adapter in the adapter's constructor (here stored as mContext). In getView, just call
((Activity) mContext).startActivityForResult(intent,REQUEST_FOR_ACTIVITY_CODE);

user936414
- 7,574
- 3
- 30
- 29
-
-
2How can we be sure that context here is always an instance of Activity? – Asakura Apr 21 '17 at 09:16
-
@Asakura As mentioned in the answer "Just pass the context of the activity to the adapter in the adapter's constructor" - you need to pass the context of activity – user936414 May 15 '17 at 05:06
-
3But how if we want to get onActivityResult in Fragment instead of Activity ? – Tony Feb 14 '19 at 11:30
9
Not necessarily pass to pass context in adapter's constructor. You can get context from parent ViewGroup. Sample for RecyclerView adapter:
Context mContext;
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
mContext = parent.getContext();
...
}
Sample for ListView BaseAdapter
Context mContext;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
mContext = parent.getContext();
...
}
And use it wherever you want
((Activity) mContext).startActivityForResult(intent, REQUEST_FOR_ACTIVITY_CODE);

eugeneek
- 812
- 9
- 24
-
1The Context does not have startActivityForResult? You need an Activity for that. – Kenneth Nov 27 '15 at 07:45
-
-
1One problem I just discovered with this approach is if you are receiving the result in a Fragment it will not be delivered. Even if you call super.onActivityResult(requestCode, resultCode, data); in your activity it does not appear to deliver the result to your fragment. In further reading here: http://stackoverflow.com/questions/6147884/onactivityresult-not-being-called-in-fragment I found that you must make the call startActivityForResult without any Activity. Thoughts? – mparkes Apr 21 '16 at 22:30
-
It because of in this case you call `startActivityForResult()` from your Activity. If you need to receive result in Fragment, you need to call `startActivityForResult()` from Fragment. – eugeneek Feb 27 '18 at 10:56
-
1But how if we want to call onActivityResult in Fragment like this case ? https://stackoverflow.com/questions/54685955/onactivityresult-not-getting-called-in-fragment-where-intent-pass-from-adapter-c?noredirect=1#comment96166329_54685955 – Tony Feb 14 '19 at 11:31
3
//First Do
public Activity context;
public int REQUEST_CODE = 111;
public Adapter(Activity context, Data data) {
this.context = context;
}
///Second Do
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, NextActivity.class);
context.startActivityForResult(intent, REQUEST_CODE);
}
});
}
If you follow this code then you don't need to write it - ((Activity) context) - every time, before - .startActivityForResult(intent, REQUEST_CODE); - when you use startActivityForResult in Adapter.

Mr. A
- 71
- 7
-
1Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. – Pouria Hemi Nov 11 '20 at 14:05
-
What do i need to do if i want to get the result from Activity onActivityResult result back to to the same Adapter postion, to update the UI (onBindViewHolder) – Rahul.S Aug 06 '21 at 08:50
-
2@Rahul.S see on https://stackoverflow.com/questions/20015950/can-we-call-startactivityforresult-from-adapterhow-to-get-the-response - this link – Mr. A Aug 19 '21 at 18:09
2
Offcource...
((Activity) context).startActivityForResult(intent, 911);
Caution !!
Only pass MyActivity.this from activity to adapter as context.
Only pass getActivity(); from fragment to adapter as context.

Ali Nawaz
- 2,016
- 20
- 30