Skip to content

Instantly share code, notes, and snippets.

@Guilherme-HRamos
Last active July 1, 2019 01:44
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/51e36160391e1b76d995dbb447640d48 to your computer and use it in GitHub Desktop.
Save Guilherme-HRamos/51e36160391e1b76d995dbb447640d48 to your computer and use it in GitHub Desktop.
Medium - Android/Java Interfaces like a Boss! - Callbacks pt ll
public class OpenLogFileActivity extends AppCompatActivity {
// some Activity code here
private void onOpenLogButtonClick() {
// Usando uma classe anônima como Callback
new OpenFileHelper("home/log.txt", new OpenFileHelperCallback() {
@Override
public void onFileOpenSuccess(File file) {
// do something with file
}
@Override
public void onFileOpenFailure(Throwable error) {
// show error to user
}
});
}
}
// Para usar o this no Callback, a classe deverá implementar o mesmo
public class OpenNotesFileActivity extends AppCompatActivity implements OpenFileHelperCallback {
// some Activity code here
private void onOpenNotesButtonClick() {
// Usando a própria classe como Callback
new OpenFileHelper("home/notes.txt", this);
}
@Override
public void onFileOpenSuccess(File file) {
// do something with file
}
@Override
public void onFileOpenFailure(Throwable error) {
// show error to user
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment