Java tutorial
/* This file is part of ZAX. ZAX is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. ZAX is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with ZAX. If not, see <http://www.gnu.org/licenses/>. */ package com.inovex.zabbixmobile.activities.fragments; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.IBinder; import android.support.v4.app.ListFragment; import android.util.Log; import com.inovex.zabbixmobile.data.ZabbixDataService; import com.inovex.zabbixmobile.data.ZabbixDataService.ZabbixDataBinder; /** * Same as {@link BaseServiceConnectedFragment}, just based on a list fragment * instead of a regular one. * */ public abstract class BaseServiceConnectedListFragment extends ListFragment implements ServiceConnection { public static final String TAG = BaseServiceConnectedListFragment.class.getSimpleName(); protected ZabbixDataService mZabbixDataService; @Override public void onStart() { super.onStart(); Log.d(TAG, "onStart"); // we need to do this after the view was created!! Intent intent = new Intent(getActivity(), ZabbixDataService.class); getActivity().getApplicationContext().bindService(intent, this, Context.BIND_AUTO_CREATE); } @Override public void onStop() { super.onStop(); getActivity().getApplicationContext().unbindService(this); } @Override public void onServiceConnected(ComponentName name, IBinder service) { Log.d(TAG, "onServiceConnected"); ZabbixDataBinder binder = (ZabbixDataBinder) service; mZabbixDataService = binder.getService(); setupListAdapter(); } @Override public void onServiceDisconnected(ComponentName name) { mZabbixDataService = null; } /** * Retrieves the list adapter from the data service. */ protected abstract void setupListAdapter(); }