Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3977086
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:52:53+00:00 2026-05-20T04:52:53+00:00

What Is Working Fine I have 2 activities in my app. First activity calls

  • 0

What Is Working Fine

I have 2 activities in my app. First activity calls second actifity for results.

Second activity shows a new layout and lets user perform certain actions. There is a “OK” button. When user presses this button, second activity is finished and user goes back to first activity.

Under the hood, first activity calls second ativity like this:

Intent intent = new Intent(this, NextAct.class);
intent.putExtra("input", input);
this.startActivityForResult(intent, 99);

On “OK” button press, second activity returns with result like this:

Intent intent = new Intent();
intent.putExtra("output", output);
setResult(RESULT_OK, intent);
finish();

After that, first activity’s onActivityResult is called with results successfully:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // requestCode is 99
    // resultCode is -1
    // data holds my output
}

Above setup is working perfectly

What Is Not Working

Now I had a simple requirement, I wanted the user to close the second activity not with an “OK” button, but in a natural way with “HARDWARE BACK” button.

I tried moving the setResult logic in onStop and onDestroy methods of second activity, but it turned out that onActivityResult of first activity is called before onStop or onDestroy methods of second activity and as a result setResult logic does not get a chance to run at all.

Then I tried moving the setResult logic in onPause method of second activity like this

protected void onPause() {
  super.onPause();
  Intent intent = new Intent();
  intent.putExtra("output", output);
  setResult(RESULT_OK, intent);
  //finish();  enabling or disabling this does not work
}

But although onPause is being called well before onActivityResult and setResult logic runs properly, still I get all null values in onActivityResult

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // requestCode is 99
    // resultCode is 0
    // data comes as null
}

I need to know why this is happening and if onResume is not a proper place to put setResult logic, what is the most natural way to do it?

Thanks much.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T04:52:54+00:00Added an answer on May 20, 2026 at 4:52 am

    Now I had a simple requirement, I wanted the user to close the second activity not with an “OK” button, but in a natural way with “HARDWARE BACK” button.

    That is not usually “natural” for a startActivityForResult() scenario. The BACK button should allow the user to tell you “No, it is not OK” (i.e., cancel).

    This does not mean you necessarily have to have an OK button. For example, if NextAct were a ListActivity, a typical pattern is for clicking on a list item to be considered “OK” (i.e., call setResult() and finish() from onListItemClick()), and BACK to mean “I didn’t really mean to start NextAct, sorry”.

    I need to know why this is happening

    You are calling setResult() too late. If the user presses BACK, by the time of onPause(), the result (RESULT_CANCELED) has already been determined.

    if onResume is not a proper place to put setResult logic

    onResume() would not be a proper place to “put setResult logic”

    what is the most natural way to do it?

    Possibly what you have with the OK button is the “most natural way to do it”.

    However, if for some strange reason you really do want the BACK button to mean “OK”, override onBackPressed() and call setResult() there before chaining to the superclass with super.onBackPressed().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have made an android app which working fine. I implemented the Login functionality
In an application I'm currently working on I have my first activity with a
I have one AlertDialog which is working fine.I have set some background images to
I have the following query which is working fine, but I also want to
I have a view which was working fine when I was joining my main
I have a site setup that is working fine in ie8 and firefox but
I have a jquery autocomplete field working fine followed by a date input field.
I have a jQuery statement that's working fine. How would I re-write it in
I have jquery validation code which is working fine in ff but the same
I have the following code that is working fine in jsfiddle - http://jsfiddle.net/darkajax/FHZBy/ I've

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.