android how to call startActivityForResult inside an adapter

13,878

Solution 1

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);
    }
});

Solution 2

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 .

Share:
13,878
user2059935
Author by

user2059935

Updated on July 28, 2022

Comments

  • user2059935
    user2059935 almost 2 years

    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 ?

  • user2059935
    user2059935 over 11 years
    I am trying it, would you wait please
  • Sudhanshu Gaur
    Sudhanshu Gaur over 8 years
    yea my second activity is opening but onActivityResult in my adapter is never getting called why is this happening ??
  • Prasad Pawar
    Prasad Pawar almost 8 years
    If someone needs it, onActivityResult() in the activity that provides the context will be called.
  • Harneet Kaur
    Harneet Kaur over 7 years
    onactivityResult called but all the ids of that view becomes null.
  • William Kinaan
    William Kinaan over 7 years
    @Preet which view?
  • Harneet Kaur
    Harneet Kaur over 7 years
    @WilliamKinaan all the ids within calling fragment. In my case i am startingactivityFor Result in Utils class by doing this ((Activity) context).startActivityForResult(intent, resultCode); When it comes to onActivityResult of Calling Fragment , all ids (textview, buttons) etc becomes null. If i call activity from Fragment then it works fine, but i need it in Utils as it is going to call at multiple places.
  • William Kinaan
    William Kinaan over 7 years
    @Preet please post a new question and let me, I'll try to help according to the Available time
  • Tony
    Tony over 5 years
    @WilliamKinaan please help stackoverflow.com/questions/54685955/…