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>
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With