Question

After adding a preferences activity to my widget I can't add it to the home screen anymore.

When I drop the widget on the screen the preferences activity shows as intended, I can use it, the settings are stored, but the only way to close it is the back button and as soon as I press it I get back to the widget selection screen, no widget added to the home screen.

The documentation has only a very limited example on that issue and every example or tutorial I can find on this is completely outdated (for SDK 1.5 or similar) where next to nothing applies anymore.

I tried to add these lines from the documentation to the onStop() method of my preferences activity, but it doesn't change the behaviour.

So, how do I close the preferences activity correctly so the WidgetManager adds the widget to the home screen?

Here are the related files:

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="de.schneidr.mytchibo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="15"
        android:targetSdkVersion="18" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <receiver android:name="de.schneidr.android.MyTchiboWidgetProvider" >
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>
            <meta-data android:name="android.appwidget.provider"
                       android:resource="@xml/mytchibo_appwidget_info" />
        </receiver>

        <activity android:name="de.schneidr.android.MyTchiboWidgetConfigure">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/>
            </intent-filter>
        </activity>

    </application>

</manifest>

appinfo_widget.xml:

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="250dp"
    android:minHeight="40dp"
    android:minResizeWidth="250dp"
    android:minResizeHeight="40dp"
    android:updatePeriodMillis="86400000"
    android:previewImage="@drawable/tcm_logo"
    android:initialLayout="@layout/mytchibo_appwidget"
    android:resizeMode="none"
    android:configure="de.schneidr.android.MyTchiboWidgetConfigure" 
    android:widgetCategory="home_screen">
</appwidget-provider> 

MyTchiboWidgetConfigure.java

package de.schneidr.android;

import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.RemoteViews;
import de.schneidr.mytchibo.R;

public class MyTchiboWidgetConfigure extends Activity {

       @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            // Display the fragment as the main content.
            getFragmentManager().beginTransaction()
                    .replace(android.R.id.content, new MyTchiboWidgetConfigureFragment())
                    .commit();
        }

       protected void onStop() {
            Intent intent = getIntent();
            Bundle extras = intent.getExtras();
            int mAppWidgetId = 0;
            if (extras != null) {
                mAppWidgetId = extras.getInt(
                        AppWidgetManager.EXTRA_APPWIDGET_ID, 
                        AppWidgetManager.INVALID_APPWIDGET_ID);
            }

            Context context = this.getApplicationContext();

            AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);

            RemoteViews views = new RemoteViews(context.getPackageName(),
                    R.layout.mytchibo_appwidget);
                    appWidgetManager.updateAppWidget(mAppWidgetId, views);

            Intent resultValue = new Intent();
            resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
            setResult(RESULT_OK, resultValue);
            finish();
           super.onStop();
       }
}
Was it helpful?

Solution

Okay, I found the solution in this answer. Basically, what I had in the OnStop() function belongs in onBackPressed().

public class MyTchiboWidgetConfigure extends Activity {
    private static String CONFIGURE_ACTION="android.appwidget.action.APPWIDGET_CONFIGURE";

    @Override
    public void onBackPressed() {
        if (CONFIGURE_ACTION.equals(getIntent().getAction())) {
            Intent intent = getIntent();
            Bundle extras = intent.getExtras();

            if (extras != null) {
                int id = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
                        AppWidgetManager.INVALID_APPWIDGET_ID);
                AppWidgetManager mgr = AppWidgetManager.getInstance(this);
                RemoteViews views = new RemoteViews(getPackageName(),
                        R.layout.mytchibo_appwidget);

                mgr.updateAppWidget(id, views);

                Intent result = new Intent();

                result.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, id);
                setResult(RESULT_OK, result);
                sendBroadcast(new Intent(this, MyTchiboWidgetProvider.class));
            }
        }

        super.onBackPressed();
    }
}

The solution is taken directly from here: https://github.com/commonsguy/cw-advandroid/blob/b01438e7f0fed8f795ddec4be43066905f03d0cc/AppWidget/TwitterWidget/src/com/commonsware/android/appwidget/TWPrefs.java

OTHER TIPS

well, not making the configuration(preferences) activity's launch mode "singleInstance" worked for me. I unknowingly added this line

        android:launchMode="singleInstance"

in my activity manifest. I deleted the line and it worked.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top