Skip to content Skip to sidebar Skip to footer

OnActivityResult NOT Called In MainActivity From Previous Activity

I know this is a basic question and I have seen multiple answers on it in stackoverflow but I seem to be stuck still. The onActivityResult is just not being called. Here is my cod

Solution 1:

For getting result back in OnActivityResult you should use startActivityForResult(Intent,REQ_CODE) method for starting your second activity.


Solution 2:

On Your MainActivity Open A Intent Like this:

Intent intent = new Intent(this,"Your c class name.class");
 startActivityForResult(intent, 1);

and in your Second Activity do this:

Intent returnIntent = new Intent();
  returnIntent.putExtra("result",result);//your changed value here
  setResult(Activity.RESULT_OK,returnIntent);
  finish();

The again in your MainActivity do this

  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {

if (requestCode == 1) {
    if(resultCode == Activity.RESULT_OK){
        String result=data.getStringExtra("result");

        //you will get the changed data here
      }
      if (resultCode == Activity.RESULT_CANCELED) {
          //Write your code if there's no result
     }
  }
  }//onActivityResult

Solution 3:

Use MainActivity.this.startActivityForResult(intent, REQUEST_CODE);


Post a Comment for "OnActivityResult NOT Called In MainActivity From Previous Activity"