Is there a way to find out when the user closes a particular preference screen? I need to do some updating when the user has changed the preferences in one of my preference screens. Not when the user clicks an actual checkbox, but rather when they press the back button and are done editing the preferences.
So far, all I've been able to find is that I can override my PreferenceActivity's onPause() event, which seems to happen when the user closes the preferences. It works, but I'm not sure this is the best way of going about it. Any suggestions?
EDIT
I couldn't figure out how to actually call a method of my main activity from within the PreferenceActivity. Tried playing around with putExtra() and all sorts of stuff. Eventually i figured - why not do the completely opposite? So instead I implemented the onResume() method in my main activity. Works great for doing stuff when the user closes the preferences, and I can live with the fact that my update method runs every time I resume my app as well.
Well I have been searching for a solution of this issue for weeks up to 2 min ago...
i think i found the way.
Preference myPrefScreen = findPreference("myPrefScreen");
myPrefScreen
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference prefScreen) {
// TODO Auto-generated method stub
Dialog prefScreenDialog = ((PreferenceScreen) prefScreen)
.getDialog();
prefScreenDialog
.setOnDismissListener(new OnDismissListener() {
#Override
public void onDismiss(DialogInterface arg0) {
// TODO Auto-generated method stub
///HERE YOU CAN EXECUTE WHATEVER YOU WANT...
}
});
return false;
}
});
You need OnPreferenceClickListener to correctly instantiate the PreferenceScreen Dialog generated only once the PreferenceScreen entry has been tapped on the screen and the new window with the preference children are shown. Otherwise you always getDialog() returns null and there's no way to attach the OnDismissListener to it.
Once you set the OnDismissListener the trick is done!!
I hope this could help.
Happy coding to all!!
That's the best way to go about it: overriding the onPause() method. It's called right before your activity leaves the foreground.
onPause is the right place to handle and update.
Related
I currently have a Settings Preference Activity consisting of one EditTextPreference. I would like to have an listener when the Dialog closes, so I could run some code
I've tried some of the methods listed here: http://developer.android.com/reference/android/preference/EditTextPreference.html,
but to no luck.
Am I missing something?
EDIT:
protected void onDialogClosed (boolean positiveResult)
{
Log.d("tag", "dialog");
}
I used this method for example wich should print the tag each time the dialog is closed, but it is not working like I thought it would
You should listen to the preference change, not to the dialog close event.
In my Android app, I use SharedPreferences to let the user manage some settings. Now after a user changes any setting, and returning to the app from the settings page, I want my Views (Fragments) to use the latest values from SharedPreferences.
The changes could include reloading a Custom View to use a color scheme or remove filtering for a List View.
Currently only when the app is restarted, the required changes are applied. I am convinced that there is a way to solve my problem, but I am unable to figure it out.
Assume that I am supporting Android 2.2 and above, so that any newer APIs for this may not be used unless its present inside the Support Library.
Not exactly sure what your question is but it seems to me that when your user changes a setting and then hits the back button to return to the fragment you are not seeing the changes? if this is the case it is because when the user goes back android reinstates the version of the fragment that was on the stack (the one before any changes were made). My suggestion would be to try moving the loading of the new shared prefs to the onResume method for the fragment. This way they should be loaded when the user goes back.
Try this
#Override
public void onResume(){
super.onResume();
setContentView(R.layout.currentFrag);
}
this should reload the page with the correct changes.
You need to use the onWindowFocusChanged Method to check if the current activity has lost focus.
#Override
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);
// Add the code to perform on the fragments after the settings have changed.
}
I think I need to put some code within my onStop method. It pertains to a service that should be running only when the activity is finished()
but when the user follows some linkify'd text to the web browser, or when the user presses the homescreen, both call onStop() but these do not end the activity, I don't want to end the activity when a user follows a link, so I can't put finish() within onStop() unless I can detect and differentiate when this happens within onStop()
is there a way I can override Linkify() so that I can add a flag within it, or maybe make it run startActivityforResult() so that I can information back in a result?
similarly, is there a way I can set the activity to finish() when the user presses the home button?
Thanks
Is it possible for you to check isFinishing() in your onStop() to decide whether you need to run the service-related code or not?
#Override
protected void onStop() {
super.onStop();
if (isFinishing()) {
// Your service-related code here that should only run if finish()
// was called.
}
}
UPDATE: (after understanding the problem better)
Very similar to my suggested approach on another question, you can probably override startActivity() to intercept when the link is launching and set your flag if that's the case.
#Override
public void startActivity(Intent intent) {
final String action = intent.getAction();
if (action.equals(Intent.ACTION_VIEW)) {
// maybe also check if getScheme() is 'http' then set our flag
persist.saveToPrefs("linkifyClick", true);
}
// proceed with normal handling by the framework
super.startActivity(intent);
}
That other answer also show how you can call startActivityForResult() too instead if you want.
SOLUTION (with still a remaining problem)
I put an onClick: attribute on my textView in the xml
<TextView
android:id="#+id/body"
android:paddingTop="10dp"
android:onClick="onTextViewClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
and then saved a flag within my sharedpreferences within that new method
public void onTextViewClick(View v){
//write to sharedpreferences
persist.saveToPrefs("linkifyClick", true);
}
and then in my onStop I can detect whether that flag is set or not!
#Override
public void onStop(){
super.onStop();
if(persist.getFromPrefs("linkifyClick", false) == false)
{
finish();
}
else
persist.saveToPrefs("linkifyClick", false); //if it was true, then set to false
}
PROBLEM
It is possible to click on the linkified text without touching the actual link portion. This sets the flag to true and still mixes up the lifecycle I am going for. So I need to be able to detect when the home button is pressed
UPDATE: this helped clarify the problem for the other poster (with the accepted answer), that person updated the answer and I accepted it after I put it in my code. It works.
Can anyone give me an example or explain me how i subscribe to the
onDialogClosed method of an EditTextPreference?
http://developer.android.com/reference/android/preference/EditTextPreference.html#onDialogClosed%28boolean%29
I want to know when a dialog had its OK button clicked
and then retrive the information from the EditTextPreference.
Is there any examples/tutorials of this available or can anyone point
me in the right direction?
Thank you.
Just for completion: Since EditTextPreference is a Preference, you can use a OnPreferenceChangeListener. That will be called when the preference is changed. Check for the EditTextPreferences key in the callback and retrieve the new value to act on it. This is especially useful when the preference can be changed in more than one place or will be changed in the background by your app (e.g. writing defaults back when clicking a "default settings" button), since every change will trigger that callback (when your register it global on your SharedPreferences). The onDialogClosed will only be triggered when the user closed the actual dialog.
If you want to watch the single preference you can also use the EditTextPreference.setOnPreferenceChangedListener() function to assign a listener to that preference only.
OnPreferenceChangeListener documentation
You can make something like that :
#Override
protected void onDialogClosed(boolean positiveResult) {
if (positiveResult) {
String text=MyEditText.getText();
}
else {
// cancel hit
}
}
I can't solve this problem. I have preference screen and there is sub-preference that opens up another screen. On that another screen change of items can be caught with OnSharedPreferenceChangeListener and I change summary in parent preference screen, but when I go back to that parent preference screen, summary did not changed.
Same question was asked here, but conclusion was not clear, and I could not solve this problem. It seems a common problem to me and I guess there is good solution for this.
Dose anyone know a solution for this problem?
There is one thing I like to keep: sub-preference is standard one, not custom.
I've solved this by adding OnPreferenceClickListener to the preferences which will change the summary in the main screen.
OnPreferenceClickListener viewUpdater = new OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference preference) {
updateView();
return false;
}
};
Within the updateView() method I'm setting the summary to a new value and then I'm using the invalidateViews method of the preferences listview to trigger an update of the displayed summary
private void updateView() {
preference.setSummary(newSummary);
getListView().invalidateViews();
}
Check the answer of #jmbouffard that's work for me