Created
January 20, 2014 01:10
-
-
Save benigumocom/8513311 to your computer and use it in GitHub Desktop.
Fragment Transaction Handler
This file contains hidden or 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 class LinesFragment extends ContextMenuSherlockListFragment implements LoaderCallbacks<List<LineInfo>>, Callback { | |
... | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
handler = new Handler(this); | |
adapter = new LinesAdapter(getActivity()); | |
} | |
@Override | |
public void onActivityCreated(Bundle savedInstanceState) { | |
super.onActivityCreated(savedInstanceState); | |
setListAdapter(adapter); | |
setListShown(false); | |
getLoaderManager().initLoader(LINES_LOADER_ID, null, this); | |
} | |
@Override | |
public Loader<List<LineInfo>> onCreateLoader(int id, Bundle args) { | |
return new LinesLoader(getActivity()); | |
} | |
@Override | |
public void onLoadFinished(Loader<List<LineInfo>> loader, List<LineInfo> data) { | |
if (data != null) { | |
adapter.setLinesList(data); | |
} else if (isResumed()) { | |
handler.sendEmptyMessage(LINES_LOADING_ERROR_WHAT); | |
} | |
// The list should now be shown. | |
if (isResumed()) { | |
setListShown(true); | |
} else { | |
setListShownNoAnimation(true); | |
} | |
} | |
@Override | |
public void onLoaderReset(Loader<List<LineInfo>> loader) { | |
adapter.setLinesList(null); | |
} | |
@Override | |
public boolean handleMessage(Message message) { | |
switch (message.what) { | |
case LINES_LOADING_ERROR_WHAT: | |
MessageDialogFragment | |
.newInstance(R.string.error_title, R.string.lines_loading_error) | |
.show(getFragmentManager()); | |
return true; | |
} | |
return false; | |
} | |
... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment