如何强制在配置活动的最新情况? [英] How do I force an update in a configuration activity?

查看:95
本文介绍了如何强制在配置活动的最新情况?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写与调用被点击了确定按钮时,下面的方法配置活动小组件:

I'm writing a widget with a configuration activity which calls the following method when its OK button is clicked:

private void ok()
{
    // ...Do Widget Configuration...

    // Force an update
    Context context = getApplicationContext();
    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
    AppWidgetManager.getInstance(context).updateAppWidget(widget_id, views);

    // Return the expected result
    Intent result = new Intent();
    result.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widget_id);
    setResult(RESULT_OK, result);

    finish();
}

这是几乎是逐字从的文档 widget_id 认为,在被挖出的小部件ID活动的的onCreate()

This is almost verbatim from the documentation. widget_id holds the widget ID that was dug up during the activity's onCreate().

当我把主屏幕上的小部件的实例,我已经验证了我得到事件的预期顺序:

When I place an instance of the widget on the home screen, I've verified that I get the expected sequence of events:

  • 的onReceive()得到一个 ACTION_APPWIDGET_ENABLED 如果这是第一个。
  • 的OnUpdate()被调用(我在其中发现了小部件未配置和借鉴的东西作为默认操作)。
  • 在配置活动出现。
  • 当我preSS 确定确定()上述方法被调用。
  • onReceive() gets an ACTION_APPWIDGET_ENABLED if it's the first one.
  • onUpdate() gets called (in which I detect that the widget isn't configured and draw something as a default action).
  • The configuration activity appears.
  • When I press OK, the ok() method above gets called.

该方法获取一路走过来的完成()和配置活动消失,但有以的onupdate没有呼叫()的onReceive()这点之后。 (小部件本身没有 updatePeriodMillis )。我最终有我的默认操作结果屏幕上的一个小部件,但从来没有得到更新。

The method gets all the way through to the finish() and the configuration activity goes away, but there's no call to onUpdate() or onReceive() after this point. (The widget itself has no updatePeriodMillis.) I end up with a widget on the screen that has the results of my default action but never gets updated.

如果我设置了窗口小部件时没有使用配置的活动,它就会被创建的时候,一切都按预期工作(只是没有配置位)进行更新。

If I set the widget up without a configuration activity, it gets updated when created and everything works as expected (just without the configured bits).

我缺少的东西在我强制更新的方式?

Am I missing something in the way I force an update?

谢谢!

推荐答案

这横空出世的工作:

new MyWidgetProviderClass()
  .onUpdate(this,
            AppWidgetManager.getInstance(this),
            new int[] { widget_id }
   );

这篇关于如何强制在配置活动的最新情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆