0

So my main Activity within the onOptionsItemSelected block has a settings option that when clicked, launches a settings activity and passes some values to it. The code looks like this

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        Intent intent = new Intent(this, SettingsActivity.class);
        intent.putExtra("SettingsObj", settings);
        intent.putExtra("UpdateDate", employees[0].updated);
        startActivityForResult(intent, 1);
        System.out.println("Did we return to the same instance????????????????");
        AdjustSettings();
    }

    return super.onOptionsItemSelected(item);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1) {
        if (resultCode == RESULT_OK) {
            System.out.println("Did we get to this POINT????????");
            settings = (Settings) data.getExtras().getSerializable("result");
            System.out.println("What is the size of text?  " + settings.TextSize);
        }
    }
}

Now the settings activity is called successfully and I pass back a Settings object afterwards. The Settings activity code passing back the value looks like this

@Override
public void onPause() {
    returnIntent.putExtra("result", settings);
    setResult(RESULT_OK, returnIntent);
    finish();

    super.onPause();
}

Now when i back out of the settings page, my main Activity does not resume at the next line printing out the question "Did we return?". The only print out code is what is at the beginning of the class. Does hitting the return arrow on my phone inside intent 2 not take me to the next line in intent 1 or is it a new instance of intent 1? Also, wasn't sure if i should return my intent data in the onPause or onStop :P Which is better? Thanks!

jay bear
  • 37
  • 1
  • 1
  • 4

1 Answers1

2

Normally, your code "Did we return..." and AdjustSettings() will be executed just after the start of your second activity (because startActivityForResult isn't a blocking call, it's just a request for the system to start the activity x). So your first activity will not wait the end of the second to execute these lines.

About the result of your second activity, check out this thread : setResult does not work when BACK button pressed. Calling setResult on the onPause() method seems to be too late (your result will be always RESULT_CANCELED), you have to do it in the onBackPressed() method. However, I'm not sure your application workflow is correct, the back button isn't designed to save settings. You should take a look at the preferences system : http://developer.android.com/guide/topics/ui/settings.html

Community
  • 1
  • 1
Nicolas Mauti
  • 506
  • 3
  • 13
  • Thanks man, very helpful information. So that is now working :) And yeah, i know i should use the preference system but i could never find any guides that explained it very well to me and i had already gotten so much of my settings programs built out by the time i even heard of preferences that i just decided to push through. :P – jay bear Jul 27 '15 at 19:55