8

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 ?

William Kinaan
  • 28,059
  • 20
  • 85
  • 118
user2059935
  • 971
  • 4
  • 12
  • 24

2 Answers2

21
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);
    }
});
William Kinaan
  • 28,059
  • 20
  • 85
  • 118
  • I am trying it, would you wait please – user2059935 Feb 11 '13 at 00:48
  • yea my second activity is opening but onActivityResult in my adapter is never getting called why is this happening ?? – Sudhanshu Gaur Feb 12 '16 at 15:02
  • If someone needs it, onActivityResult() in the activity that provides the context will be called. – Prasad Pawar Sep 10 '16 at 10:36
  • onactivityResult called but all the ids of that view becomes null. – Harneet Kaur Oct 25 '16 at 09:54
  • @Preet which view? – William Kinaan Oct 25 '16 at 09:58
  • @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. – Harneet Kaur Oct 25 '16 at 10:01
  • @Preet please post a new question and let me, I'll try to help according to the Available time – William Kinaan Oct 25 '16 at 10:45
  • @WilliamKinaan please help https://stackoverflow.com/questions/54685955/onactivityresult-not-getting-called-in-fragment-where-intent-pass-from-adapter-c?noredirect=1#comment96166108_54685955 – Tony Feb 14 '19 at 11:25
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 .

katmanco
  • 1,146
  • 12
  • 24