Options are: using MainActivity.this
or passing the context through the Runnable constructor.
1st option:
public class MainActivity extends AppCompatActivity {
//...
public void onButtonClick() {
new Thread(new Runnable() {
@Override
public void run() {
SomeMethod(MainActivity.this);
}
}).start();
}
//...
}
2nd option:
public class MainActivity extends AppCompatActivity {
//...
public void onButtonClick() {
new Thread(new SomeRunnable(this)).start();
}
//...
private class SomeRunnable implements Runnable {
private final Context context;
public SomeRunnable(Context context) {
this.context = context;
}
@Override
public void run() {
SomeMethod(context);
}
}
}
The first option seems a bit more convenient (as it's simply shorter), but could there be any behaviour problems with such code?