Java tutorial
/* * Copyright (C) 2013-2015 Mike Cunningham * * 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 com.emetophobe.permissionviewer.fragments; import android.content.SharedPreferences; import android.database.Cursor; import android.os.Bundle; import android.preference.PreferenceManager; import android.support.annotation.Nullable; import android.support.v4.app.ListFragment; import android.support.v4.app.LoaderManager; import android.support.v4.content.Loader; import android.support.v4.widget.CursorAdapter; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ViewFlipper; import com.emetophobe.permissionviewer.R; import butterknife.ButterKnife; import butterknife.InjectView; public abstract class AbstractListFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor>, SharedPreferences.OnSharedPreferenceChangeListener { @InjectView(R.id.flipper) protected ViewFlipper mFlipper; protected CursorAdapter mAdapter; private SharedPreferences mSharedPrefs; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_abstractlist, container, false); } @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); ButterKnife.inject(this, view); } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); mSharedPrefs = PreferenceManager.getDefaultSharedPreferences(getActivity()); mSharedPrefs.registerOnSharedPreferenceChangeListener(this); } @Override public void onDestroy() { super.onDestroy(); mSharedPrefs.unregisterOnSharedPreferenceChangeListener(this); } @Override public void onSharedPreferenceChanged(SharedPreferences sharedPrefs, String key) { getLoaderManager().restartLoader(0, null, this); } @Override public Loader<Cursor> onCreateLoader(int id, Bundle args) { return null; } @Override public void onLoadFinished(Loader<Cursor> loader, Cursor data) { mAdapter.swapCursor(data); setLoading(false); } @Override public void onLoaderReset(Loader<Cursor> loader) { mAdapter.swapCursor(null); } /** Show or hide the loading screen. */ protected void setLoading(boolean show) { mFlipper.setDisplayedChild(show ? 0 : 1); } }