Call a method when click on preference header Android -
i call method clear cache when click on specific header in preference screen in android.
the problem onsharedpreferencechanged never called :
here piece of code of preferences.xml :
<header android:icon="@drawable/ic_action_cancel" android:title="vider le cache d'articles" android:key="clearcache" android:summary="clear datas."> </header>
and, here code of settingactivity :
package com.rss.preferences; import java.io.file; import java.util.list; import com.rss.r; import android.content.context; import android.content.sharedpreferences; import android.content.sharedpreferences.onsharedpreferencechangelistener; import android.os.bundle; import android.preference.preferenceactivity; import android.preference.preferencemanager; import android.util.log; import android.view.gravity; import android.widget.textview; public class settingsfragment extends preferenceactivity { sharedpreferences settings; public static final string key_clear_cache = "clearcache"; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); // add button header list. if (hasheaders()) { textview txt = new textview(this); txt.settext("version 1.0 beta"); txt.setgravity(gravity.center); setlistfooter(txt); } settings = getsharedpreferences("clearcache", 0); settings.registeronsharedpreferencechangelistener(new sharedpreferences.onsharedpreferencechangelistener() { @override public void onsharedpreferencechanged (sharedpreferences sharedpreferences, string key) { log.d("info", "popopo"); } }); } public void onbuildheaders(list<header> target) { loadheadersfromresource(r.xml.preferences, target); } // public void onsharedpreferencechanged(sharedpreferences sharedpreferences, string key) { // if (key.equals(key_clear_cache)) { // clearapplicationdata(); // } // } public void clearapplicationdata() { file cache = getcachedir(); file appdir = new file(cache.getparent()); if (appdir.exists()) { string[] children = appdir.list(); (string s : children) { if (!s.equals("lib")) { deletedir(new file(appdir, s)); log.i("tag", "**************** file /data/data/app_package/" + s + " deleted *******************"); } } } } public static boolean deletedir(file dir) { if (dir != null && dir.isdirectory()) { string[] children = dir.list(); (int = 0; < children.length; i++) { boolean success = deletedir(new file(dir, children[i])); if (!success) { return false; } } } return dir.delete(); } }
you don't need onsharedpreferencechangelistener in case. need override onheaderclick method in preferenceactivity.
@override public void onheaderclick(header header, int position) { super.onheaderclick(header, position); if (header.id == r.id.clear_cache) { clearapplicationdata(); } }
of course have add id header in xml.
<header android:id="@+id/clear_cache" android:icon="@drawable/ic_action_cancel" android:title="vider le cache d'articles" android:key="clearcache" android:summary="clear datas."> </header>
Comments
Post a Comment