2

I am implementing an app related to widgets in this I prepared one widget and that's working perfectly and when click on views that opens the activity to prepare settings for that widget.

But my requirement is when click on home screen widget I want to open directly activity and then widget.

From the above picture when click on Custom Analog Clock widget open first Activity in my application and then I want to show the widget because from my activity I will setup some settings for that widget. How can I open activity first?

NagarjunaReddy
  • 8,621
  • 10
  • 63
  • 98

2 Answers2

6

Widgets can have a special activity called when they are added to the home screen. That activity can return a SUCCESS intent with the widget ID receive in the "onCreate" method of the activity or RESULT_CANCELED if you want to prevent the creation of the widget itself.

You just need to add the configure activity class in a android:configure attribute at the appwidget-provider XML and add an intent filter with android.appwidget.action.APPWIDGET_CONFIGURE to your activity declaration at the AndroidManifest. It's detailed in http://developer.android.com/guide/topics/appwidgets/index.html#Configuring

The intent received has the widget ID:

public void onCreate(Bundle savedInstanceState) {
    ...
    int widgetID = getIntent().getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
    ...
}

and then you can either finish your activity with setResult(RESULT_CANCELED); or confirm the widget creation with:

    Intent resultValue = new Intent();
    resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetID);
    setResult(RESULT_OK, resultValue);

You can do all your widget settings from that activity.

On the other hand, if you want to perform other setup that only needs to occur once for all your widgets instances, you can make then in the onEnabled method of your AppWidgetProvider class as described in http://developer.android.com/guide/topics/appwidgets/index.html#AppWidgetProvider

vggonz
  • 1,986
  • 1
  • 14
  • 14
  • where i have to get the int widgetId? i mean in which activity i have to declare? – NagarjunaReddy Apr 15 '13 at 08:54
  • The widget ID is received inside an intent in the "configure" activity i've explained above. You usually use it to associate some kind of configuration just for that widget (store it in SharedPreferences for example). – vggonz Apr 15 '13 at 10:33
  • In the main activity for your widget, you will receive the same widget ID on the `onUpdate` method, so you could easily get the data you associated with that specific object from the configure activity. – vggonz Apr 15 '13 at 10:34
  • i have widget id in configure activity and that same id i have to pass for main widget activity. how can shows that widget? where is the relation between configure and widget activity? – NagarjunaReddy Apr 15 '13 at 12:24
  • The typical usage of widgets and configure activities is something like this: 1. You add the widget to your home screen and your configure activity shows. 2. Your configure activity receives a widget ID. You can display and setup anything you need. Then, finish that activity with a successful result intent. 3. Your widget is shown on your home screen and the widget activity is called. The `onUpdate` method from your AppWidgetProvider is called and receive the widget ids. 4. From there you can, for example open a new activity with an `onClick` event. – vggonz Apr 15 '13 at 12:33
  • well said.. thanks for ur answer. now i have only one doubt i.e. configure activity recieved id's done. next that id have to pass for the AppWidgetProvider activity. how can recieve the same id? because i created two or more widgets then all widgets showing the same results on screen – NagarjunaReddy Apr 15 '13 at 13:36
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/28254/discussion-between-nagarjunareddy-and-vggonz) – NagarjunaReddy Apr 15 '13 at 13:40
  • how can we get same id in both configure activity and appwidget provider activity? otherwise it shows same values for all widgets when i created two or more widgets? – NagarjunaReddy Apr 16 '13 at 04:59
  • See http://stackoverflow.com/questions/13534421/android-widget-id-only-for-current-widget – vggonz Apr 16 '13 at 07:21
  • actually i don't want that one. we get one id at configure activity how to use that id in app widget provider?let us join in chat i have some doubts plz.. – NagarjunaReddy Apr 16 '13 at 07:27
  • anyway i got solution from my own way. But ur's point of direction more helped me. thanks for your support. – NagarjunaReddy Apr 17 '13 at 08:41
1

You should check out this tutorial: http://android-er.blogspot.nl/2010/10/simple-home-screen-app-widget-with.html It does exactly what you want.

Mdlc
  • 7,128
  • 12
  • 55
  • 98