Reputation: 39
I created a android widget and when I added a configure activity the widget launch a kind of activity and close it but the widget don't show, Its obviously something wrong with the code in my Configure class:
package com.rb.widget;
import android.app.Activity;
import android.os.Bundle;
public class WidgetConfig extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
}
How should a configure class look like ?
Upvotes: 1
Views: 339
Reputation: 6682
In onCreate
function, you should call
/**
* In onCreate() we have to ensure that if the user presses BACK or cancelled the activity,
* then we should not add app widget.
*/
setResult(RESULT_CANCELED);
to ensure that if you close config activity the widget will not add to home. And following code shows how to config a widget.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/**
* In onCreate() we have to ensure that if the user presses BACK or cancelled the activity,
* then we should not add app widget.
*/
setResult(RESULT_CANCELED);
setContentView(R.layout.activity_widget_settings);
Intent intent = getIntent();
Bundle extras = intent.getExtras();
if (extras != null){
appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
}
if (appWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID){
finish();
return;
}
appWidgetManager = AppWidgetManager.getInstance(this);
views = new RemoteViews(this.getPackageName(), R.layout.my_app_widget);
}
After you config, don't forget to call:
Intent widgetIntent = new Intent();
widgetIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
setResult(RESULT_OK,widgetIntent);
appWidgetManager.updateAppWidget(appWidgetId, views);
Upvotes: 1