Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return from preference screen to main activity?

I have one main activity and one preferenceActivity. On my first activity I call menu and go on preferenceActivity by calling startActivityForResult.

                case R.id.settings:
                startActivityForResult(new Intent(this, SettingsActivity.class), LAUNCH_SETTINGS);
                return true;

Then I change my settings and want to return on main activity and see main activity with new settings applyed. In onPause() method do following (as I right understand this method will be called when I press back button, right?)

   @Override
     protected void onPause() {
         super.onPause();
         setResult(RESULT_OK, new Intent(this, MainActivity.class));
         finish();
     }

On main activity

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == LAUNCH_SETTINGS) {
            if (resultCode == RESULT_OK) {
                new RefreshList().execute(ACTION_SELECT);
                Log.d(TAG, "On activity result");
            }
        }
    }

But my acyncTask do not called and log not printed. How correctly I can do this? Thanks!

like image 579
Georgy Gobozov Avatar asked Oct 14 '10 18:10

Georgy Gobozov


1 Answers

On my first activity I call menu and go on preferenceActivity by calling startActivityForResult.

Using startActivityForResult() with a PreferenceActivity is rather unusual. PreferenceActivity is designed to be used with startActivity(). If the one starting the PreferenceActivity cares about preference changes, it should register a preference change listener with the SharedPreferences object.

Then I change my settings and want to return on main activity and see main activity with new settings applyed.

I recommend that you use a SharedPreferences.OnSharedPreferenceChangeListener instead. Or, simply re-read the preferences you care about in the original activity's onStart() or onResume() method.

In onPause() method do following (as I right understand this method will be called when I press back button, right?)

No, that will not work. onPause() is too late to call setResult().

like image 55
CommonsWare Avatar answered Nov 01 '22 12:11

CommonsWare