title: Is it possible to access a method in MainActivity from a Homescreen Widget?
body: <p>I made a homescreen widget for my app. I need it to communicate with methods in my MainActivity.</p>
  
  <p>How would I go about it?</p>
  
  <p>The dev doc mentions "control widgets" but does not explain how to implement them.(<a href="https://developer.android.com/guide/topics/appwidgets/overview#control-widgets" rel="nofollow noreferrer">https://developer.android.com/guide/topics/appwidgets/overview#control-widgets</a>).</p>
  
  <p>Do I need a service or an interface to communicate between the widget and the MainActivity?</p>
  
  <p>What I've tried:</p>
  
  <p>I have tried passing my click counter value from my WidgetProvider OnReceive method to my MainActivity using SharedPreferences. This does work to some extent, but it would need to be real time communication both ways, so it's not suitable. That's why I was thinking in terms of a service. But would there be a better option?</p>
  
  <p>And if I use a service, would it be a specific one? Like a "RemoteViewsService" or an "IntentService"?</p>
  
  <p>Here is an example of my code in my WidgetProvider:</p>
  
  <pre><code>    @Override
      public void onReceive(Context context, Intent intent) {
          super.onReceive(context, intent);
  
          if (ACTION_SIMPLE_APPWIDGET.equals(intent.getAction())) {
              if (mCounter &lt; optionsStrings.length - 1) {
                  mCounter++;
              } else {
                  mCounter = 0;
              }
  
              RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.app_widget);
  
              stringText = optionsStrings[mCounter];
              views.setTextViewText(R.id.appwidget_text, stringText);
  </code></pre>
  
  <p>Depending on the counter value, I need to trigger various methods in MainActivity.</p>