Reputation: 3752
I am trying to reload my MainActivity after I do something in my SettingsActivity. My Up button in SettingsActivity works great and reloads MainActivity when returned from SettingsActivity. But back button saves the data but doesn't reload MainActivity when presses inside SettingsActivity. Basically my getSupportLoaderManager().restartLoader() doesn't work with onBackPressed() Do you know, how can I fix it ?
My MainActivity is something like:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.action_settings:
Intent intent = new Intent(this, SettingsActivity.class);
startActivityForResult(intent, SETTINGS_REQUEST_CODE);
break;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == SETTINGS_REQUEST_CODE && resultCode == RESULT_OK){
getSupportLoaderManager().restartLoader(0, null, this);
}
}
And my SettingsActivity is something like:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == android.R.id.home){ //WORKS GREAT
finishSetting();
}
return super.onOptionsItemSelected(item);
}
@Override
public void onBackPressed() { //SAVES DATA BUT DOESN'T RELOAD MAINACTIVITY
finishSetting();
//startActivity(new Intent(this, MainActivity.class));
}
private void finishSetting(){
if(newLanguageSelection==null || newLanguageSelection.equals(getLanguagePreference())){
setResult(RESULT_CANCELED);
}
else {
setLanguagePreference(newLanguageSelection);
setResult(RESULT_OK);
}
finish();
}
Upvotes: 0
Views: 1009
Reputation: 191
You should override your onResume()
to check if the main activity is refreshed,so whenever you come back from setting activity to main activity .. it will get updated..
Upvotes: 1
Reputation: 15
In public void onBackPressed()
method just write :
getActionBar().setDisplayHomeAsUpEnabled(true);
Upvotes: 0
Reputation: 2781
When you started the second activity, your Main is paused, and when the back button is pressed, it is resumed...
On your SettingsActivity
, set a flag (such as using a SharedPreferences)
SettingsActivity.onBackPressed(){
finishSetting();
SharedPreferences prefs= getSharedPreferences("prefs", MODE_PRIVATE);
prefs.edit().puBoolean("isShouldReload", true).apply();
}
Then, on MainActivity.onResume
onResume(){
SharedPreferences prefs= getSharedPreferences("prefs", MODE_PRIVATE);
if(prefs.getBoolean("isShouldReload", false)){
// Do your reload.
}
}
Upvotes: 1