Skip to content

Instantly share code, notes, and snippets.

@Guilherme-HRamos
Last active September 15, 2020 18:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Guilherme-HRamos/2d2533e11c1ddcb91a79341c94992682 to your computer and use it in GitHub Desktop.
Save Guilherme-HRamos/2d2533e11c1ddcb91a79341c94992682 to your computer and use it in GitHub Desktop.
Medium - Android/Java Interfaces like a Boss! - Callbacks pt l
public class OpenFileHelper {
private final String filePath;
private final OpenFileHelperCallback callback;
public OpenFileHelper(String filePath, OpenFileHelperCallback callback) {
this.filePath = filePath;
this.callback = callback;
init();
}
private void init() {
try {
openFile();
} catch (Exception error) {
error.printStackTrace();
callback.onFileOpenFailure(error);
}
}
private void openFile() throws IOException, FileNotFoundException {
File file = new File(filePath);
// open file code here
// ...
callback.onFileOpenSuccess(file);
}
}
public interface OpenFileHelperCallback {
void onFileOpenSuccess(File file);
void onFileOpenFailure(Throwable error);
}
Copy link

ghost commented Sep 15, 2020

Ok

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment