Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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