31 maggio 2013

Android development: shortcut vs widget

In the next version of LabelToDo there will be the first widget, an icon 1x1 that allows user to launch a "non-launcher activity" of the application. In particular widget launches the activity of Tasks, displaying only the tasks with the label that the user has chosen when he created the widget.

I raised the issue of how to do the widget, initially planning to build a "real widget". Many applications use this solution, but I soon realized that it is not the best solution.

How explained here from an Android framework engineer "Widgets should look like widgets, not like shortcuts". Here is the solution, create a shortcut and not a widget, moreover in the latest versions of the Android shortcuts appear in the same menu of widgets and they are widgets for users.

What are the advantages of a shortcut than a widget to create an icon 1x1 that launches an activity?

- Efficiency, a shortcut does the shortcut job better than a widget
- Graphics, the shortcut has its label and its icon and aligns with the other icons on the home better than a widget that has to be adapted and normally has no label
- Writing code, it is not necessary to adjust the widget, shortcut's code is quiet simple

After this long introduction let's see how to create a shortcut that:
- the user can add to the home screen
- launches an activity
- passes parameters to launched activity

The necessary steps are:
1. create the activity launched when the user adds the shortcut
2. add the previous activity to manifest.xml
3. write the code of the previous activity, in the simplest case activity is invisible to the user, it just serves to create a shortcut. There are cases, like mine, in which the activity requires instead an interaction with user to configure the shortcut before creating it

In each case the activity will:
a. create the intent that will launch the target activity on shortcut clic
b. create another intent that do the shortcut
c. add to previous intent the shortcut's description and icon

All the way here to the theory, in the next post we will see a little code.

Nessun commento:

Posta un commento