2

I have only a single Activity called MainActivity. It has a function which has to called from another class which is in a different module

public class MainActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}

 @Override
      public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}


public void tag_proc(String s1)
{

    System.out.println("working i guess"+s1)

}
}

This function has to be called from another class from another module

public class NewCls {
       MainAcitivity mact= new MainActivity();

      public void dothis()
       {
  String s="new";
       mact.tag_proc(s);

              }

 }

This cannnot be done because they are in different modules. What is the best and easy solution for this. If interface is a solution, how best to use it

Vis
  • 109
  • 7

2 Answers2

0

Not really clear what exactly you want to achieve but never do this:

MainAcitivity mact= new MainActivity();

You only create Activities through Intents, not yourself.

If you want a tightly coupled module you just pass it the activity in the constructor e.g.

public NewCls(MainActivity activity) {
    myActivityReference = activity
}

public void dothis() {
    ...
    myActivityReference.tag_proc(s);
}

It would be best if you extract an interface and use this instead of the activity class itself, but for simplicity I showed it the trivial way.

If you want a more loosly coupled communication you can use local broadcasts. Just register a receiver in the activity and send the broadcast from NewCls like this

public void dothis() {
  Intent intent = new Intent("custom-event-name");
  intent.putExtra("myString", "This is my message!");
  LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
}

and in your Activity

@Override
public void onCreate(Bundle savedInstanceState) {

  ...

  // Register to receive messages. We are registering an observer (mMessageReceiver) to receive Intents with actions named "custom-event-name".
  LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
      new IntentFilter("custom-event-name"));
}

// Our handler for received Intents. This will be called whenever an Intent with an action named "custom-event-name" is broadcasted.
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
  @Override
  public void onReceive(Context context, Intent intent) {
    String s1= intent.getStringExtra("myString");
    tag_proc(s1);
  }
};

@Override
protected void onDestroy() {
  // Unregister since the activity is about to be closed.
     LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
      super.onDestroy();
}

See more on broadcasts in this question.

Community
  • 1
  • 1
Patrick
  • 33,984
  • 10
  • 106
  • 126
0

In my opinion, calling method like this could be a bad practice. You can make the method static, but you should decide if this is right for this case. I think the best choice might be using EventBus, which is effective and has simple API. For example: EventBus from greenrobot.

wttek
  • 173
  • 1
  • 7