6

How can i make a callback to an Activity form a Java Class?

Example:

    public class TestClass{
    String text = "Test";
    public TestClass(Context context){
            startActivity(new Intent(context, SomeActivity.class));
    }

    private void sendToSomeActivity(){
      //Call some method of SomeActivity and pas text as string
    }

   }

When sendToSomeActivity() is called, i want to make a callback to the already started SomeActivity and pass some text to the Activity. In SomeActivity i want to use the text.

Note: The TestClass object that i want to use is already created in another class.

How can this be done?

user3494179
  • 263
  • 2
  • 3
  • 15

3 Answers3

6

The solution I chose is as follows:

Use BroadcastReceivers to communicate between Java classes and Activities.

Example:

public class SomeActivity extends Activity{
    private MyBroadcastReceiver receiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        receiver = new MyBroadcastReceiver();
        this.registerReceiver(receiver, new IntentFilter(MyBroadcastReceiver.ACTION));
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.unregisterReceiver(receiver);
    }

    private class MyBroadcastReceiver extends BroadcastReceiver{
        public static final String ACTION = "com.example.ACTION_SOMETHING"
        @Override
        public void onReceive(Context context, Intent intent) {
            String test = intent.getStringExtra("dataToPass");
        }
    }
} 

public class TestClass{
    private String test = "TEST";
    private Context context;

    public TestClass(Context context){
      this.context = context;
    }
    private void sendToSomeActivity(){
       Intent intent = new Intent();
       intent.setAction(SomeActivity.MyBroadcastReceiver.ACTION);
       intent.putExtra("dataToPass", test);
       context.sendBroadcast(intent); 
    }

}
user3494179
  • 263
  • 2
  • 3
  • 15
0

In your java class create an interface like this

public class TestClass{


    private MyInterface myInterface;

    public interface OnSendSomething {
        public void onSending(String sendWhateverYouWant);
    }

    public void setOnSendListener(MyInterface myInterface) {
        this.myInterface = myInterface;
    }

}


private void sendToSomeActivity(){
      //Call some method of SomeActivity and pas text as string

     myInterface.onSending(sendWhateverYouWant);
}

And in your activity do something like this:

TestClass tclass = new TestClass(context);

tclass.setOnSendListener(new OnSendSomething () {
    @Override
    public void onSending(String sendWhateverYouWant) {

      //sendWhateverYouWant is here in activity

    }
});

You can also visit these links for better understanding.

How to create our own Listener interface in android?

Observer Design Pattern in Java

Community
  • 1
  • 1
Androider
  • 2,884
  • 5
  • 28
  • 47
  • I already have an instance of TestClass that i want to use. The intstance is created in another java class. Creating an instance of TestClass in SomeActivity Woudn't work. – user3494179 Mar 25 '15 at 14:29
  • you need to have a java class instance in the activity where you want to send data. – Androider Mar 26 '15 at 06:15
0

Try this..

public class TestClass{

interface Implementable{
  public void passData(String text);
}

Implementable imple; 

String text = "Test";
public TestClass(Context context){
        startActivity(new Intent(context, SomeActivity.class));
}

private void sendToSomeActivity(){
  if(imple != null){
   imple.passData(text);
  }
}

public void setListener(Implementable im){
  imple = im;
  }

}

class SomeActivity implements Implementable{

new TestClass().setListener(this);

@override
public void passData(String text){
//here is your text
  }
}
Sjd
  • 1,261
  • 1
  • 12
  • 11
  • I already have an instance of TestClass that i want to use. The intstance is created in another java class. Creating an instance of TestClass in SomeActivity Woudn't work. – user3494179 Mar 25 '15 at 14:29