Android小部件需要返回两次

时间:2011-06-16 17:52:36

标签: android android-activity widget provider back

我做了一个非常简单的小部件,当按下时启动一个活动,但由于某种原因,当我按下活动关闭,我看到主屏幕,但它完全没有响应,直到我再次按回来。我不知道为什么会发生这种情况我的唯一猜测是,也许这是小部件提供者也在活动背后打开这里的代码我正在使用任何想法为什么我必须按两次退出我的小部件启动的活动非常感谢

如果重要,小部件也在一个单独的包中

清单

<receiver android:name="com.gflam.widget.WidgetProvider" >
        <intent-filter>
        <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
    </intent-filter>
    <meta-data android:name="android.appwidget.provider"
           android:resource="@xml/widget" />
    </receiver>
<activity android:name="com.gflam.widget.Dialog" android:launchMode="singleInstance" android:noHistory="true" ></activity>

WidgetProvider

public class WidgetProvider extends AppWidgetProvider {

    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        final int N = appWidgetIds.length;

        for (int i=0; i<N; i++) {
            int appWidgetId = appWidgetIds[i];

            Intent intent = new Intent(context, com.gflam.widget.Dialog.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
            views.setOnClickPendingIntent(R.id.widget, pendingIntent);

            appWidgetManager.updateAppWidget(appWidgetId, views);
        }
    }
}

活动已启动

public class Dialog extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SharedPreferences settings = getSharedPreferences(PREFS_DEVICE, 0);

        final CharSequence[] items = {"1", "2", "3", "4", "5"};
        AlertDialog.Builder builder = new AlertDialog.Builder(Dialog.this);
        builder.setTitle("What would you like to do?");
        builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
            }
            });
            builder.setPositiveButton("Okay", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    SparseBooleanArray CheCked =((AlertDialog)dialog).getListView().getCheckedItemPositions();
                    if(CheCked.get(0)){

                    }
                    if(CheCked.get(1)){

                    }
                    if(CheCked.get(2)){

                    }
                    if(CheCked.get(3)){

                    }
                    if(CheCked.get(4)){

                    }
                }
            }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.dismiss();
                }
        }).show();
    }
}

2 个答案:

答案 0 :(得分:2)

    Dialog dialog = new Dialog(this);
    dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialog) {
            YourActivity.this.finish();
        }
    });

Override dialog onBackPressed()?

答案 1 :(得分:0)

想出它很有意义第一个回来杀死对话然后我需要第二个回来杀死活动所以我想我需要重写它来扩展对话框和什么不抱歉的问题,但确实找出了什么最终错了