/*
* Copyright (c) 2016 Ha Duy Trung
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.github.hidroh.materialistic;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBar;
import android.support.v7.preference.PreferenceFragmentCompat;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
public class PreferencesActivity extends ThemedActivity {
public static final String EXTRA_TITLE = PreferencesActivity.class.getName() + ".EXTRA_TITLE";
public static final String EXTRA_PREFERENCES = PreferencesActivity.class.getName() + ".EXTRA_PREFERENCES";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_preferences);
setTitle(getIntent().getIntExtra(EXTRA_TITLE, 0));
setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
//noinspection ConstantConditions
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME |
ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_TITLE);
if (savedInstanceState == null) {
Bundle args = new Bundle();
args.putInt(EXTRA_PREFERENCES, getIntent().getIntExtra(EXTRA_PREFERENCES, 0));
getSupportFragmentManager()
.beginTransaction()
.add(R.id.content_frame,
Fragment.instantiate(this, SettingsFragment.class.getName(), args),
SettingsFragment.class.getName())
.commit();
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
public static class SettingsFragment extends PreferenceFragmentCompat {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Preferences.sync(getPreferenceManager());
}
@Override
public void onCreatePreferences(Bundle bundle, String s) {
addPreferencesFromResource(getArguments().getInt(EXTRA_PREFERENCES));
}
}
}