0

I am using the preferenceActivy class to manage my preferences. But I need to close the whole preferences from a fragment. Source code is cut to a minimal example

public class VPNPreferences extends PreferenceActivity {
   public boolean onOptionsItemSelected(MenuItem item) {
       finish(); // This should close fragment + Preferences list
   }

   public boolean onCreateOptionsMenu(Menu menu) {
       getMenuInflater().inflate(R.menu.vpnpreferences_menu, menu);
      return super.onCreateOptionsMenu(menu);
   }

}

I use the new 3.2+ API which display as two column on tables and as sperate Activities on mobile phone. The menu icon is visible on both the list and the Activity with the fragment.

When the button button is pressed the activy should close and if the activity is the fragment was called from the Settings list that setting list should close too. Unfortunately finish() only closes the current fragment Activity.

plaisthos
  • 6,255
  • 6
  • 35
  • 63

2 Answers2

1

What I do is registered a BroadcastReceiver in each of my preference activites, and all it does is call finish(). When I want to close the settings, I send a broadcast with that action.

Jason Robinson
  • 31,005
  • 19
  • 77
  • 131
  • It's a clean hack at least :) – Jason Robinson Oct 01 '12 at 15:05
  • I accept this answer since there seems to be no other. I implemented this a similar hack checking in onResume() if the profile I want to show is still there or has been deleted... For reference: http://code.google.com/p/ics-openvpn/source/detail?r=1d194d4f22ca5cbf47d5915889366c6d27c66362 should anyone stuble an on this again. – plaisthos Oct 02 '12 at 11:16
0

What I do is call

NavUtils.navigateUpFromSameTask(getActivity());

Inside the fragment where I have changed a setting in am OnPreferenceChangeListener for instance.

Also you need to set who is the PreferenceActivity parent in manifest.xml

Anexon
  • 113
  • 1
  • 10