call custom Activity as startActivityForResult

后端 未结 2 611
一生所求
一生所求 2021-02-14 16:11

I am starting an Activity for result as

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

ActivityA is started. There is a

相关标签:
2条回答
  • 2021-02-14 16:20

    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");
        }
    
    0 讨论(0)
  • 2021-02-14 16:25

    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");
              }
           }
    }
    
    0 讨论(0)
提交回复
热议问题