Last active
October 4, 2019 02:07
-
-
Save billthefarmer/ccdce123232a4cf1d51a635ab6bb7668 to your computer and use it in GitHub Desktop.
Android - Use a singleton for data retention and background task
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
// On create | |
@Override | |
public void onCreate(Bundle savedInstanceState) | |
{ | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.main); | |
// Get data instance | |
instance = Data.getInstance(this); | |
// On resume | |
@Override | |
protected void onResume() | |
{ | |
super.onResume(); | |
// Get data instance, connect callbacks | |
instance = Data.getInstance(this); | |
// Check data instance | |
if (instance != null) | |
{ | |
// Get the saved select list | |
List<Integer> list = instance.getList(); | |
// Get the saved value map | |
valueMap = instance.getMap(); | |
} | |
// Start the task | |
if (instance != null) | |
instance.startParseTask(URL);; | |
} | |
// On pause | |
@Override | |
protected void onPause() | |
{ | |
super.onPause(); | |
// Save the select list and value map in the data instance | |
if (instance != null) | |
{ | |
instance.setList(selectList); | |
instance.setMap(valueMap); | |
} | |
// Disconnect callbacks | |
instance = Data.getInstance(null); | |
} |
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
// Data class | |
public class Data | |
{ | |
private static Data instance; | |
private Map<String, Double> map; | |
private List<Integer> list; | |
private TaskCallbacks callbacks; | |
// Constructor | |
private Data() | |
{ | |
} | |
// Get instance | |
public static Data getInstance(TaskCallbacks callbacks) | |
{ | |
if (instance == null) | |
instance = new Data(); | |
instance.callbacks = callbacks; | |
return instance; | |
} | |
// Set list | |
public void setList(List<Integer> list) | |
{ | |
this.list = list; | |
} | |
// Get list | |
public List<Integer> getList() | |
{ | |
return list; | |
} | |
// Set map | |
public void setMap(Map<String, Double> map) | |
{ | |
this.map = map; | |
} | |
// Get map | |
public Map<String, Double> getMap() | |
{ | |
return map; | |
} | |
// Start parse task | |
protected void startParseTask(String url) | |
{ | |
ParseTask parseTask = new ParseTask(); | |
parseTask.execute(url); | |
} | |
// ParseTask class | |
protected class ParseTask | |
extends AsyncTask<String, String, Map<String, Double>> | |
{ | |
// The system calls this to perform work in a worker thread | |
// and delivers it the parameters given to AsyncTask.execute() | |
@Override | |
protected Map doInBackground(String... urls) | |
{ | |
// Get a parser | |
Parser parser = new Parser(); | |
// Start the parser and report progress with the date | |
if (parser.startParser(urls[0]) == true) | |
publishProgress(parser.getDate()); | |
// Return the map | |
return parser.getMap(); | |
} | |
// On progress update | |
@Override | |
protected void onProgressUpdate(String... date) | |
{ | |
if (callbacks != null) | |
callbacks.onProgressUpdate(date); | |
} | |
// The system calls this to perform work in the UI thread and | |
// delivers the result from doInBackground() | |
@Override | |
protected void onPostExecute(Map<String, Double> map) | |
{ | |
if (callbacks != null) | |
callbacks.onPostExecute(map); | |
} | |
} | |
// TaskCallbacks interface | |
interface TaskCallbacks | |
{ | |
void onProgressUpdate(String... date); | |
void onPostExecute(Map<String, Double> map); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment