Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can we call startActivityForResult from adapter?

is it possible to have method onActivityResume within adapter & call startActivityForResult?

like image 603
napster Avatar asked Aug 09 '12 11:08

napster


People also ask

What can I use instead of startActivityForResult?

But recently startActivityForResult() method is deprecated in AndroidX. Android came up with ActivityResultCallback (also called Activity Results API) as an alternative for it.

What is request code startActivityForResult?

The request code is any int value. The request code identifies the return result when the result arrives. ( You can call startActivityForResult more than once before you get any results. When results arrive, you use the request code to distinguish one result from another.

Can startActivityForResult still be used?

onActivityResult , startActivityForResult , requestPermissions , and onRequestPermissionsResult are deprecated on androidx.


2 Answers

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); 
like image 99
user936414 Avatar answered Sep 24 '22 22:09

user936414


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); 
like image 37
eugeneek Avatar answered Sep 24 '22 22:09

eugeneek