Skip to content

Instantly share code, notes, and snippets.

@aindong
Last active February 8, 2017 10:23
Show Gist options
  • Save aindong/06737ccaa482d5c9e79391fae03173bf to your computer and use it in GitHub Desktop.
Save aindong/06737ccaa482d5c9e79391fae03173bf to your computer and use it in GitHub Desktop.
// Create a new interface
public interface UIUpdatable {
public void updateUI(String message);
}
// Implement the new interface on the activity
public class QuestionNew extends AppCompatActivity implements UIUpdatable {
public TextView txtView;
@Override
protected void onCreate(Bundle savedInstanceState) {
txtView = findViewById(R.id.txt_view);
// inject this activity to the async test
new CCListener(this).execute(new String[]{"AnyData"}); // start your task
}
// method that comes from the interface
@Override
public void updateUI(String str) {
txtView.setText(str);
}
}
// Your async task
public class CCListener extends AsyncTask<Void, Void, Void> {
UIUpdatable uiUpdatable;
// Constructor, dependency injection
public AsyncTest(Activity activity){
uiUpdatable = (UIUpdatable)activity;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
uiUpdatable.updateUI(result);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment