pouzzler
pouzzler

Reputation: 1834

Android: App Widget Configuration Activity does not behave like Activity (Edited from: Android: AppWidget not embedding into home screen)

(Edit here - Original post below)

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();
  }
}

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().

Any clues?

Best regards

(Original post here)

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.

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();
        }
    }

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

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.

EDIT: 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.

Upvotes: 4

Views: 1975

Answers (2)

JRaymond
JRaymond

Reputation: 11782

Calling finish from onPause is a little bit odd, I would think it might work, provided you called super.onPause() appropriately. Better, though, is for when you launch the activity in the first place, Set the activity flags to noHistory like so:

intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);

This finishes you activity for you every time it's going to be hidden by another activity, and onPause will be called naturally as a result of the activity lifecycle. As I said, though, the most common error (at least for me, I do it all the time) is forgetting to call super.onPause() at the end of your onPause method.

Upvotes: 0

hackbod
hackbod

Reputation: 91331

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.

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.

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.

Upvotes: 1

Related Questions