Send plain text from Android library to Android project

I am trying to send some text from my library to an android project. They are connected, I can start to wash my face from the project, but I do not understand how to send data from the library to the main project. My question is: is this possible?

Thank.

+2
source share
1 answer

You can achieve this interaction through interfaces.
The idea is to create a callback method that will be implemented in your activity and will be called by the project library to send the text back to you (in the android project).

For example, create an interface:

public interface OnTaskFinishedListener(){
   void onTaskFinished(String text);
}

, Task doTask(), Android - , .

Task :

public class Task{
   // ......

   public void doTask(OnTaskFinishedListener listener){
      // Do the task...

      String textToSend = // some text to send to caller activity
      listener.onTaskFinished(textToSend);
   }
}

OnTaskFinishedListner:

public class MainActivity extends Activity implements OnTaskFinishedListener{

   @Override
   public void onCreate(Bundle savedInstanceState){
       // .........
       Task task = new Task();
       task.doTask(this); //pass the current activity as the listener.
   }

   @Override
   public void onTaskFinished(String result){
      // do what you need to do with "result"
      // the method will be called when "doTask()" will finish its job.
   }
} 
+4

Source: https://habr.com/ru/post/1653263/


All Articles