Created
December 5, 2015 18:20
-
-
Save hrules6872/c3f4f607d2509c6aedd4 to your computer and use it in GitHub Desktop.
BasePreferenceActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class BasePreferenceActivity extends PreferenceActivity { | |
private AppCompatDelegate appCompatDelegate; | |
private Toolbar toolbar; | |
protected abstract int getPreferencesResource(); | |
protected abstract int getLayoutResource(); | |
protected abstract int getTitleResource(); | |
public Toolbar getToolbar() { | |
return toolbar; | |
} | |
@Override protected void onCreate(Bundle savedInstanceState) { | |
getDelegate().installViewFactory(); | |
getDelegate().onCreate(savedInstanceState); | |
super.onCreate(savedInstanceState); | |
setContentView(getLayoutResource()); | |
addPreferencesFromResource(getPreferencesResource()); | |
toolbar = (Toolbar) findViewById(R.id.toolbar); | |
if (toolbar != null) { | |
setSupportActionBar(toolbar); | |
getSupportActionBar().setTitle(getTitleResource()); | |
getSupportActionBar().setHomeButtonEnabled(true); | |
getSupportActionBar().setDisplayHomeAsUpEnabled(true); | |
getSupportActionBar().setDisplayShowTitleEnabled(true); | |
} | |
} | |
@Override public boolean onOptionsItemSelected(MenuItem item) { | |
switch (item.getItemId()) { | |
case android.R.id.home: | |
onBackPressed(); | |
break; | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
@Override protected void onPostCreate(Bundle savedInstanceState) { | |
super.onPostCreate(savedInstanceState); | |
getDelegate().onPostCreate(savedInstanceState); | |
} | |
public ActionBar getSupportActionBar() { | |
return getDelegate().getSupportActionBar(); | |
} | |
public void setSupportActionBar(@Nullable Toolbar toolbar) { | |
getDelegate().setSupportActionBar(toolbar); | |
} | |
@Override public MenuInflater getMenuInflater() { | |
return getDelegate().getMenuInflater(); | |
} | |
@Override public void setContentView(@LayoutRes int layoutResID) { | |
getDelegate().setContentView(layoutResID); | |
} | |
@Override public void setContentView(View view) { | |
getDelegate().setContentView(view); | |
} | |
@Override public void setContentView(View view, ViewGroup.LayoutParams params) { | |
getDelegate().setContentView(view, params); | |
} | |
@Override public void addContentView(View view, ViewGroup.LayoutParams params) { | |
getDelegate().addContentView(view, params); | |
} | |
@Override protected void onPostResume() { | |
super.onPostResume(); | |
getDelegate().onPostResume(); | |
} | |
@Override protected void onTitleChanged(CharSequence title, int color) { | |
super.onTitleChanged(title, color); | |
getDelegate().setTitle(title); | |
} | |
@Override public void onConfigurationChanged(Configuration newConfig) { | |
super.onConfigurationChanged(newConfig); | |
getDelegate().onConfigurationChanged(newConfig); | |
} | |
@Override protected void onStop() { | |
super.onStop(); | |
getDelegate().onStop(); | |
} | |
@Override protected void onDestroy() { | |
super.onDestroy(); | |
getDelegate().onDestroy(); | |
} | |
public void invalidateOptionsMenu() { | |
getDelegate().invalidateOptionsMenu(); | |
} | |
private AppCompatDelegate getDelegate() { | |
if (appCompatDelegate == null) { | |
appCompatDelegate = AppCompatDelegate.create(this, null); | |
} | |
return appCompatDelegate; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment