Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid finishActivity always causing onActivityResult to get RESULT_CANCELED
    primarykey
    data
    text
    <p>I have a main Activity A that can kick off sub-Activities B, C, or D. I start them using startActivityForResult(newIntent, REQUEST_CODE);</p> <p>The user can choose to navigate between B, C, and D once one is displayed. Activity A controls that navigation. When they choose swap between screens 'A' first calls finishActivity(REQUEST_CODE); on the one displayed and then call startActivityForResult(newIntent, REQUEST_CODE); for the next one.</p> <p>In my onActivityResult I've got</p> <pre><code> protected void onActivityResult(int requestCode, int resultCode, Intent data) { // these logs are just for my debugging Log.w(this.toString(), "onActivityResult returned to by " + requestCode); Log.w(this.toString(), "result code = " + resultCode ); // the Activity set this value if it was returning normally if (RESULT_OK == resultCode) { Log.i(this.toString(), "---- the sub-activity returned OK ----"); // do some stuff }// end if (RESULT_OK == resultCode) // Else we got here because the user hit the back button or something went // went wrong (RESULT_CANCELED). Either way we do not want to display this // blank screen to the user. Take them back to the device connection screen. else if (RESULT_CANCELED == resultCode) { finish(); } } </code></pre> <p>For some reason I'm getting back a zero or RESULT_CANCELED everytime even though in my sub-activities B,C,D I only set that result if the user chooses to hit the back button (I display an "are you sure" dialog and they choose to exit. In B,C,D I have the following in onPause. The userExiting is a flag I set from the Exit dialog.</p> <pre><code>protected void onPause() { Log.i(this.toString(), "onPause"); // hack to try to setResult for an activity whose finishActivty was called if ( !this.exiting ) { Log.i(this.toString(), "======== RESULT_OK ========"); Intent returnIntent = new Intent(); setResult(RESULT_OK, returnIntent); finish(); } displayed = false; super.onPause(); } </code></pre> <p>Any ideas? </p> <p>Edit - to be clear the issue is that after calling finishActivty on the old Activity, since a result was never set, Activity A always thinks it should exit. One weird side items. This doesn't happen until the user navigates two times. In other words B is displayed and they choose to go to C. Works, but I can see in the log that onActivityResult hasn't been called. User selects to go from C to D, boom, I see two calls to onActivtyResult, 'A; exits and D is left displayed and the user is unable to nav away.</p> <p>Update: Since this has been getting a lot of views I thought I'd post this update. The project evolved such that sometimes a single Activity is displays and sometimes a group are displayed as tabs. When the user packs out of the tab in some cases data needed to be returned to the Activity that kicked off the tabs. Before I added the code below RESULT_CANCELED would always be returned. Also make sure that setResult is called before finish. </p> <pre><code> if (getParent() == null) { setResult(Activity.RESULT_OK, intent); } else { getParent().setResult(Activity.RESULT_OK, intent); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload