Search code examples
androidandroid-fragmentsandroid-preferences

AndroidRestart Activity from PreferenceFragment


I declared my PreferenceFragment within a SettingsActivity like this

public class ChordsSettings extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getFragmentManager().beginTransaction()
            .replace(android.R.id.content, new MyPreferenceFragment())
            .commit();
}

public static class MyPreferenceFragment extends PreferenceFragment {
    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.settings_preferences);
    }
}
}

I did it like this and didn't create a Fragment on top of the MainActivity because I need to be able to use the back button to get from the SettingsActivity to the MainActivity and this seamed like the only way to go to achieve that.

I need to restart my MainActivity once the preferences changed.

I tried sending a Broadcast from the preferenceFragment but sendBroadcast() cannot be used from a static context. Is there any other way I can achieve this?


Solution

  • Your MainActivity should probably look like this. Note that when btnGoToSetting is clicked finish() method is called. This is for closing the currrent Activity.

    MainActivity.java

    public class MainActivity extends Activity {
    
        Button btnGoToSetting;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            btnGoToSetting = (Button)findViewById(R.id.btnGoToSetting);
            btnGoToSetting.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                    Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
                    startActivity(intent);
                }
            });
        }
    
    }
    

    SettingsActivity.java

    public class SettingsActivity extends Activity {
    
        Button btnSavePreferences;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_login);
    
            btnSavePreferences = (Button)findViewById(R.id.btnSavePreferences);
            btnSavePreferences.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //Here is where you save all your preferences
                    yourSaveFunction();
                    finish();
                    Intent intent = new Intent(SettingsActivity.this, MainActivity.class);
                    startActivity(intent);
                }
            });
        }
    
        @Override
        public void onBackPressed() {
            finish();
            Intent intent = new Intent(SettingsActivity.this, MainActivity.class);
            startActivity(intent);
        }
    
    }
    

    Note : There is another way to resfresh you data in MainActivity by making your data asynchronous so that everytime the data changes it will be refresh automatically.