I think the best way to implement this is to completely disconnect your views (your activity) from your logical class. This can be done using the interface .
, , . mainActivity textView . LogicWithTextUpdate textView mainActivity .
public class MainActivity extends Activity implements MyViewListener {
LogicWithTextUpdate logicWithTextUpdate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
logicWithTextUpdate = new LogicWithTextUpdate(this);
logicWithTextUpdate.doSomeLogic(2);
}
@Override
public void onUpdateTextListener(String text) {
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(text);
}
}
. MainActivity textview, "listener" onUpdateTextListener. :
public interface MyViewListener {
public void onUpdateTextListener(String text);
}
, :
public class LogicWithTextUpdate {
MyViewListener myViewListener;
public LogicWithTextUpdate(MyViewListener myViewListener) {
this.myViewListener = myViewListener;
}
public void doSomeLogic(int a) {
a = a * 2;
a = a + 1;
myViewListener.onUpdateTextListener(String.valueOf(a));
}
}
doSomeLogic , . myViewListener.onUpdateTextListener, onUpdateTextListener .
. - LogicWithTextUpdate Actitivy. .