This question is a kind of followup on this unanswered question with more and better details.
Basically, I have an android main activity showing some data which are taken from a database (via SQLiteDatabase
). Now I have a PreferenceActivity
which is shown when the user wants to change some preference, or delete the content of the database. In the latter case the content of the database is deleted - but how do I make the main activity to be updated? If I look at the main activity after I pressed the button to delete the content of the database, I expect to show the main activity zero entries!
Here is the relevant part of the main activity:
public class MainActivity extends AppCompatActivity { SharedPreferences.OnSharedPreferenceChangeListener mPrefListener; private SharedPreferences settings; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent intent = new Intent(this, AlarmService.class); startService(intent); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); settings = PreferenceManager.getDefaultSharedPreferences(this); mPrefListener = new SharedPreferences.OnSharedPreferenceChangeListener() { @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { Log.d("TEST", "testtest"); Toast.makeText(MainActivity.this, "TOAST MAINACTIVITY", Toast.LENGTH_LONG).show(); finish(); startActivity(getIntent()); } }; // Register the listener on the SharedPreferences settings.registerOnSharedPreferenceChangeListener(mPrefListener);
Here is the content of UserSettingActivity.java
:
public class UserSettingActivity extends PreferenceActivity { private Preference myPreference; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.preferences); myPreference = findPreference("reset"); myPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { public boolean onPreferenceClick(Preference arg0) { AlertDialog.Builder alertDialog = new AlertDialog.Builder(UserSettingActivity.this); alertDialog.setMessage("Are you sure to delete the database?"); alertDialog.setCancelable(true); alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { final DBAdapter db = new DBAdapter(UserSettingActivity.this); db.open(); db.resetDatabase(); } }); alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); alertDialog.show(); return false; } }); } }
And finally, the xml file describing the layout of the settings in xml/preferences.xml
:
<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <EditTextPreference android:title="Your Name" android:key="username" android:summary="Please provide your username"></EditTextPreference> <CheckBoxPreference android:title="Application Updates" android:defaultValue="false" android:summary="This option if selected will allow the application to check for latest versions." android:key="applicationUpdates" /> <Preference android:key="reset" android:title="Reset database" android:summary="This will remove every entry in the database" /> </PreferenceScreen>
When I click on the button reset
on the Settings screen and confirm this choice, the database is deleted resetDatabase
does a DROP
and a CREATE TABLE
of the two tables in the database). However, the Toast
'TOAST MAINACTIVITY' is never shown on the display!
What do I miss here to make this work as I expect it to work? How do I trigger a method/function/something in my main activity from the UserSettingActivity
?
No comments:
Post a Comment