user3494179
user3494179

Reputation: 373

Android make callback to an Activity from java class

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?

Upvotes: 6

Views: 15315

Answers (3)

user3494179
user3494179

Reputation: 373

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); 
    }

}

Upvotes: 6

Androider
Androider

Reputation: 2804

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

Upvotes: 0

Sjd
Sjd

Reputation: 1261

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
  }
}

Upvotes: 0

Related Questions