Skip to content Skip to sidebar Skip to footer

Onactivityresult Is Not Called When The Back Button In Actionbar Is Clicked

Here is my problem: Create a MainActivity. Add a button which will start another activity SecondActivity. Intent i = new Intent(getActivity(),SecondActivity.class);

Solution 1:

Here is the code which is working:

publicbooleanonOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
        case android.R.id.home:
            // back buttonIntentresultIntent=newIntent();
            setResult(Activity.RESULT_OK, resultIntent);
            finish();
            returntrue;

    }
    returnsuper.onOptionsItemSelected(item);
}

I guess the finish() will close the current Activity, and return true inform that action has been processed. (The default back action seems to be different from finish().)

Solution 2:

Try this:-

publicbooleanonOptionsItemSelected(MenuItem item) {

switch (item.getItemId()) {
    case android.R.id.home:
        IntentresultIntent=newIntent();
        setResult(Activity.RESULT_OK, resultIntent);
        onBackPressed();
        returntrue;
}
returnsuper.onOptionsItemSelected(item);
}

Solution 3:

Good answer is Gopal Rao code in the same question. Its worked for me. This is a copy of his solution:

publicbooleanonOptionsItemSelected(MenuItem item) {
       if (item.getItemId() == android.R.id.home) {
          Intent result = newIntent((String) null);
          result.putExtra("SOME_CONSTANT_NAME", true);
          setResult(RESULT_OK, result);
          finish();
          returntrue;
       } 
       else {
          returnsuper.onOptionsItemSelected(item);
       }
    }

Post a Comment for "Onactivityresult Is Not Called When The Back Button In Actionbar Is Clicked"