call custom Activity as startActivityForResult

I am starting an Activity for result as

startActivityForResult(new Intent(this,ActivityA.class),REQUEST_CODE)

ActivityA is started. There is a gridview on ActivityA, I want to get the position of selected image in method onActivityResult(int requestCode, int resultCode, Intent data) of caller Activity but I am not getting the way to do that

Answers


In Activity A,

onItemClick() of GridView

//create a new intent...
Intent intent = new Intent();
intent.putInt("position",position);
setResult(RESULT_OK,intent);
//close this Activity...
finish();

in Caller Activity,

  @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent){
        super.onActivityResult(requestCode, resultCode, intent);
        Bundle extras = intent.getExtras();
        if(extras != null)
        int position = extras.getInt("position");
    }

To uniquely identify your startActivityForResult() callback , check for similarity and success of Activity A . Follow this:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent){
    super.onActivityResult(requestCode, resultCode, intent);
    if(requestCode==REQUEST_CODE)
      {
        if(resultCode==RESULT_OK)
          {
              Bundle extras = intent.getExtras();
              if(extras != null)
              int position = extras.getInt("position");
          }
       }
}

Need Your Help

How to bind keyboard events to div elements?

javascript jquery

Is there a way to listen to keyboard events on a DIV element?

Is it possible to override a non-virtual method?

c# override overloading

Is there any way to override a non-virtual method? or something that gives similar results (other than creating a new method to call the desired method)?