6

I am searching for hours again and did not find an answer I understood/was looking for.

I habe an preference screen, that opens when the user clicks settings in the menu. This works. But how do I best enable the user to close this screen, when he is finished setting up.

I like the way it is done in Chrome, where you can return to the previous screen.

Other possibilities are appreciated as well.

Activity, which falls the preference (to which it should return):

   public class MainActivity extends Activity
   {
      @Override
      public void onCreate(Bundle savedInstanceState)
      {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
      }

      public void startGame(View view) 
      {
          Intent intent = new Intent(this, Game.class);
          startActivity(intent);
      }

          @Override
      public boolean onCreateOptionsMenu(Menu menu) 
      {
          MenuInflater inflater = getMenuInflater();
          inflater.inflate(R.layout.game_settings, menu);
          return super.onCreateOptionsMenu(menu);
      }

      @Override
      public boolean onOptionsItemSelected(MenuItem item) 
      {
          switch (item.getItemId()) 
          {
         case R.id.action_settings:
           getFragmentManager().beginTransaction()
              .replace(android.R.id.content, new SettingsFragment())
              .commit();
           return true;

         default:
           return super.onOptionsItemSelected(item);
          }
      }
  }

Preferences:

   public class SettingsFragment extends PreferenceFragment 
   {
       @Override
       public void onCreate(Bundle savedInstanceState) 
       {
           super.onCreate(savedInstanceState);
           addPreferencesFromResource(R.layout.preferences);
       }
   }

XML:

  <?xml version="1.0" encoding="utf-8"?>
  <PreferenceScreen 
    xmlns:android="http://schemas.android.com/apk/res/android" >
    <PreferenceCategory android:title="@string/game_settings">
      <ListPreference 
        android:title="@string/circle_setting_title"
        android:key="circle_setting"
        android:summary="@string/circle_setting_summary" 
        android:entries="@array/circle_setting_amount"
        android:entryValues="@array/circle_setting_amount_value"
        android:defaultValue="3"/>
      <ListPreference 
        android:title="@string/color_setting_title"
        android:key="color_setting"
        android:summary="@string/color_setting_summary" 
        android:entries="@array/color_setting_amount"
        android:entryValues="@array/color_setting_amount_value"
        android:defaultValue="3"/>
    </PreferenceCategory>
  </PreferenceScreen>

4 Answers4

11

The accepted answer is not correct. By doing: startActivity(new Intent(this, PrefsActivity.class)); you actually add a new activity to your stack.

If you wish to go back a screen you should use the onBackPressed method.

code example should be as follows:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == android.R.id.home) {
        getActivity().onBackPressed();
        return true;
    }
    return super.onOptionsItemSelected(item);
}

The above solution will set the home button on the action bar to react the same as the return button. Both button will go back on the activity's stack to the SettingsActivity (One Activity before).

Avi Levin
  • 1,868
  • 23
  • 32
  • 1
    This is correct and should be the accepted answer. The PreferenceActivity class already has the implementation to manage adding/removing PreferenceFragments from the stack, so we need not rewrite it. – ravindu1024 Jan 11 '17 at 23:40
1

In your code, you have:

  @Override
  public boolean onOptionsItemSelected(MenuItem item) 
  {
      switch (item.getItemId()) 
      {
     case R.id.action_settings:
       getFragmentManager().beginTransaction()
          .replace(android.R.id.content, new SettingsFragment())
          .commit();
       return true;

     default:
       return super.onOptionsItemSelected(item);
      }
  }

Instead, you could extend it as follows:

  @Override
  public boolean onOptionsItemSelected(MenuItem item) 
  {
      switch (item.getItemId()) 
      {
     case R.id.action_settings:
       getFragmentManager().beginTransaction()
          .replace(android.R.id.content, new SettingsFragment())
          .addToBackStack("settings")
          .commit();
       return true;

     default:
       return super.onOptionsItemSelected(item);
      }
  }

Now, the transaction will be remembered and reversed when someone presses the back button.

transporter_room_3
  • 2,583
  • 4
  • 33
  • 51
  • I was not specific enough. Sorry. I mean the upper legt button which calls the activity set as parent in the manifest. The standard return button works fine, but some users prefer the other button. – Ricardo di Stefano Jan 11 '14 at 14:31
0

To make MainActivity the parent Activity of Preferences:
Assuming you already have your prefs.xml (or whatever you like to name it) file in your res/xml folder:

Declare your activities in your Manifest (in the application section):

<!-- Main activity -->
<activity
    android:name="com.example.app.MainActivity"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

<!-- Preferences -->
<activity
    android:name="com.example.app.PrefsActivity"
/>

Then, assuming you have a menu, a button, or whatever you'd like to show your PreferenceScreen with, just add this line to the event handler:

startActivity(new Intent(this, PrefsActivity.class));
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • I had to add an activity between Main and Fragment, but it works. – Ricardo di Stefano Jan 11 '14 at 14:15
  • Sure, I skipped the part that it was a PreferenceFragment... I assumed it was a PreferenceActivity. So, You added a PreferenceActivity that opens the Fragment, which is OK. ;) – Phantômaxx Jan 11 '14 at 14:18
  • How to let it return to the last activity, when the user clicks the return button in the upper legt corner, if i implement settings on different Activities? – Ricardo di Stefano Jan 11 '14 at 14:22
  • By tapping the back button, the PreferenceScreen will close and you will return to the last opened fragment in the backstack – Phantômaxx Jan 11 '14 at 14:31
0

Instead of create a new activity, simply override the layout of preference fragment and inflate a layout with button inside. Then listen on the button clicked event to close the fragment.