Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android how to call startActivityForResult inside an adapter

I have an adapter class :

public class AdapterAllAddress extends BaseExpandableListAdapter {
private Context context;
    public AdapterAllAddress(Context context,
            ArrayList<AllAddressesGroup> groups) {
        // TODO Auto-generated constructor stub
        this.context = context;
    }
}

I want to call startActivityForResult when a button click , I know I can call startActivity like this:

context.startActivity() 

but i am looking for activity with results, how please ?

like image 851
user2059935 Avatar asked Feb 11 '13 00:02

user2059935


People also ask

How do I call onActivityResult in Recyclerview adapter?

How do I call onActivityResult in Recyclerview adapter? create a public method in ImageAdapter class and pass data using myImageAdapter object. – Jaydeep Devda. write onActivityResult override method in showImages Activity and inside result ok pass data to adapter using myImageAdapter.

What is the replacement for startActivityForResult?

But recently startActivityForResult() method is deprecated in AndroidX. Android came up with ActivityResultCallback (also called Activity Results API) as an alternative for it. Step 1: You just have to create an ActivityResultLauncher and pass following parameters handle onActivityResult in it as shown above.

How can I get result from startActivityForResult?

The android startActivityForResult method, requires a result from the second activity (activity to be invoked). In such case, we need to override the onActivityResult method that is invoked automatically when second activity returns result.

Can startActivityForResult still be used?

It has deprecated startActivityForResult in favour of registerForActivityResult . It was one of the first fundamentals that any Android developer has learned, and the backbone of Android's way of communicating between two components.


2 Answers

yourButton.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
        Intent intent = new Intent(context, YourNewActivity.class);
        ((Activity) context).startActivityForResult(intent, resultCode);
    }
});
like image 173
William Kinaan Avatar answered Sep 21 '22 21:09

William Kinaan


I just wanted to point a detail which i faced in my case E/ActivityThread(31584): Performing stop of activity that is not resumed: {com.example.test/activities.MainActivity} most probably you are passing getApplicationContext() to the adapter's constructor . In order to avoid this you must provide "CallingActivity.this" to the adapter's constructor as the context object , keep this in mind .

like image 28
katmanco Avatar answered Sep 22 '22 21:09

katmanco