How to pass values from a Class to Activity - Android
You can use Callback for this purpose.
Define some interface like
public interface MyCustomInterface(){
public void sendData(String str);
}
Now let your Activity implement this interface.
public class MyActivity implements MyCustomInterface {
@Override
public void sendData(String str){
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
recived_message.setText(str);
}
});
}
}
Now in UDPServer.java, write the following code
public class UDPServer {
private MyCustomInterface interface;
UDPServer(MyCustomInterface interface){
this.interface = interface;
}
}
Now whenever you have some data available lets say a string, you can send it like this
interface.sendData(str);
You have an A activity and B one, when you finish actions on B activity side you need it to effect A side when you come back.
Create an
Instance
Class and a method that u type u need, let's say;public interface SelectedBirthday { void onSelectedData(String date); }
Now we are on B side, Create an instance of your Interface Class
private SelectedBirthday mCallback;
Override
@Override public void onAttach(Activity activity) { super.onAttach(activity); try { mCallback = (SelectedBirthday) activity; } catch (ClassCastException e) { Log.d("MyDialog", "Activity doesn't implement the ISelectedData interface"); } }
Now upload the value you needed
String userBirth = (day + " " + month + " " + year); mCallback.onSelectedData(userBirth);
Ok let's go to A side
Implement our Interface Class
implements SelectedBirthday
it will warn you for its method and you implemented it
@Override
public void onSelectedData(String date) {
if (!date.equals("")) {
txt_poup_age.setText(date);
//now you are free to do what you want with the value you received automaticaly
}
}