安卓应用程序窗口小部件配置活动并不像活动(:安卓:从编辑AppWidget没有嵌入到主屏幕)

问题描述:

(编辑在这里 - 原帖如下图)

(Edit here - Original post below)

我已经缩小了原来的问题是:home键根本就不是一个定期活动和appwidget配置活动的行为相同。 我做了以下几个最基本的活动测试:

I have narrowed the original problem to this: the home key simply doesn't behave the same for a regular activity and an appwidget configuration activity. I made a test with the following minimal activity:

public class TestActivity extends Activity {    
  Logger logger = Logger.getLogger("logger");   
  @Override
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState);
  }
  @Override
  protected void onUserLeaveHint() {
    logger.info("userleave");
    super.onUserLeaveHint();
  }
}

创建这个简单的活动一个简单的应用程序按预期工作。创建最简单的小工具,没有功能性可言,使用这项活动作为一个应用程序窗口小部件配置活动,在LogCat中没有得到我们的logger.info()。

Creating a simple app with this simple activity works as intended. Creating the simplest possible widget with no functionality at all, using this activity as an App Widget Configuration Activity, the LogCat doesn't get our logger.info().

任何线索?

最好的问候

(原帖点击这里)

我有一个工作窗口小部件组成的按钮发射活动。其中之一是一个配置活动,这将是合适的两倍作为一个应用程序窗口小部件配置活动。 - 当用户嵌入的小部件到他们家的屏幕即称之为

I have a working widget consisting of buttons launching activities. One of these is a configuration activity, and it would be fitting to double as an "App Widget Configuration Activity" - i.e. call it when the user embeds the widget into their home screen.

继Android开发指南,我说这code将设置活动:

Following the Android dev guide, I added this code to the settings activity:

Intent intent = getIntent();
    Bundle extras = intent.getExtras();
    if (extras != null) {
        int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
        if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
            AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
            RemoteViews views = new RemoteViews(this.getPackageName(), R.layout.widgetlayout);
            appWidgetManager.updateAppWidget(appWidgetId, views);
            Intent resultValue = new Intent();
            resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
            setResult(RESULT_OK, resultValue);
            finish();
        }
    }

加在的onCreate(),该活动不会出现 - 很符合逻辑,因为它调用完成(),和其他一切工作按计划

Added in onCreate(), the activity doesn't appear - quite logical, since it calls finish(), and everything else works as intended.

我想要做的,就是添加它的onPause(),因为它是令人愉悦的用背部或home键退出一个配置活动,如缺乏的储存并退出按钮意味着自动保存。 加入的onPause()的小部件不会被嵌入到主屏幕的。 LogCat中不显示任何东西比信息更严重。

What I want to do, is add it in onPause(), because it's pleasing to quit a configuration activity by using the back or home button, as the absence of a "save & exit" button implies automatic saving. Added in onPause(), the widget doesn't get embedded to the home screen. LogCat doesn't show anything more grievous than info.

编辑: 缩小到涉及的onPause()的问题和硬件home键。硬件后退按钮,让窗口小部件被嵌入到主屏幕。

Narrowed to an issue involving onPause() and the hardware home button. The hardware back button, lets the widget gets embed in the Home Screen.

如果任何人有一个提示,我会非常高兴,感谢你在前进。

If anyone has a hint, I'd be most pleased, thank you in advance.

是第一个问题:为什么你想比,用户会希望该平台的标准约定您的应用程序插件的工作方式不同?一个应用程序窗口小部件,是要提供一个配置UI正常的经验是,pressing从回或家庭将取消添加。

The first question is: why do you want to make your app widget work differently than the standard convention in the platform which the user will expect? The normal experience for an app widget that is going to provide a configuration UI is that pressing back or home from that will cancel the addition.

这工作的方式是,当主机启动配置活动,它等待活动的结果,确认该小部件应添加。一个活动的默认结果是RESULT_CANCELED,该主机采用意味着它应该取消添加。因此,要允许添加小部件,你必须调用的setResult()与RESULT_OK你完成前()被调用,以交付结果返回给主机。

The way this works is that when the host launches the configuration activity, it waits for an activity result confirming that the widget should be added. The default result of an Activity is RESULT_CANCELED, which the host takes to mean it should cancel the addition. So, to allow your widget to be added, you must call setResult() with a RESULT_OK before your finish() is called in order to deliver that result back to the host.

通常这是一个坏主意,调用finish()中的onPause(),因为在onPause()可以调用的原因有很多。该回调并不意味着用户已经明确地离开你的活动,它只是意味着它正在被暂停。

Generally it is a bad idea to call finish() in onPause(), since onPause() can be called for many reasons. That callback doesn't mean the user has explicitly left your activity, it just means it is being paused.