1

I would like to call a specific method from my MainActivity on click of a widget button in my AppWidgetProvider. I have already set up the layout which only has one button in it and already created the widget.xml. My problem is that nothing happens when I click the button I want to call the methods from My MainActivity from.

This is my AppWidgetProvider class

public class NewAppWidget extends AppWidgetProvider {

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

        final int N = appWidgetIds.length;
        for (int i = 0; i < N; i++) {
            updateAppWidget(context, appWidgetManager, appWidgetIds[i]);
        }
    }

    @Override
    public void onEnabled(Context context) {

    }

    @Override
    public void onDisabled(Context context) {

    }

    static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
                                int appWidgetId) {
        CharSequence widgetText = context.getString(R.string.appwidget_text_balance);

        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);

        appWidgetManager.updateAppWidget(appWidgetId, views);

    }

    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
    }
}

And this is my MainActivity.class

public class MainActivity extends AppCompatActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
     public void makeText(String message){
        Toast.makeText(this,message,Toast.LENGHT_SHORT).show();

     }
     public void calculateData(){

       //Something in here
     }
  }

and this is my AndroidManifest.xml

<activity
    android:name=".MainActivity"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<!--declare new widget-->
<receiver android:name=".NewAppWidget" >
    <intent-filter>
        <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
    </intent-filter>

    <meta-data
        android:name="android.appwidget.provider"
        android:resource="@xml/new_app_widget_info" />
</receiver>
Vladimir Markeev
  • 654
  • 10
  • 25
  • Why not Declare calculateData() in different class and this function take some argument to do the calculation and you can call this new class anywhere , in MainActivity or in your widget class , But still you can try MainActivity obj=new MainActivity(); and call your function like this obj.calculateData(); – Sanjeev Jul 27 '15 at 08:15

1 Answers1

1

Basically what you are asking for is to share a method between 2 classes, am I right?

There are quite a few approaches to this, it's just like sharing any other data.

Use a class that can be accessed globally:

i.e -

public class GlobalMethods{

    public static void calculateData(){
        ....
    }

}

and access it from within your other class file:

GlobalMethods.calculateData(...);

I'm guessing you want to call calculateData() from within your mainActivity because there may be data in that class you want to edit.

If this is the case, maybe you should consider sharing the data between activities / classes using an interface or a singleton.

Look at some examples here on how this is done.

Not all examples will apply to your situation, but you should get some ideas from there.

Community
  • 1
  • 1