androidandroid-appwidgethuawei-mobile-servicesappwidgetproviderxiaomi

AppWidget crashes launcher after configuration activity finish (Xiaomi and Huawei only)


I'm trying to display an AppWidget on device home. I can't figure why, only on Xiaomi and Huawei devices it doesn't work.

When I drag the widget on the home screen, it nicely open my config activity. I close that activity using :

private void saveConfig() {
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);

    ComponentName thisAppWidget = new ComponentName(getPackageName(), 
        ConfigActivity.class.getName());

    Intent updateIntent = new Intent(this, ConfigActivity.class);

    int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidget);

    updateIntent.setAction(ACTION_APPWIDGET_UPDATE);
    updateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);

    setResult(RESULT_OK, updateIntent);

    finish();
}

After that, the launcher seems buggy : screen become black, recent app drawer is open, I'm not on the same page I was...

There is nothing special in the LogCat.

Everything is fine one other devices like Samsung's or OnePlus.

Does someone had similar problem ?

Manifest.xml

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@drawable/ic_launcher_round"
        android:usesCleartextTraffic="true"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        tools:ignore="GoogleAppIndexingWarning"
        tools:targetApi="m">

        <activity
            android:name="com.######.presentation.activity.ConfigActivity"
            android:screenOrientation="portrait"
            android:configChanges="orientation|keyboardHidden">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
            </intent-filter>
        </activity>

       <activity
            android:name=".presentation.activity.HomeActivity"
            android:screenOrientation="portrait"
            android:configChanges="orientation|keyboardHidden"/>

       <activity
            android:name=".presentation.activity.LegalNoticeActivity"
            android:screenOrientation="portrait"
            android:configChanges="orientation|keyboardHidden"/>

       <receiver
            android:name="com.######.provider.EnergyMixWidgetProvider"
            android:icon="@drawable/ic_launcher_round"
            android:label="@string/app_name">
        <intent-filter>
        <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
    </intent-filter>

    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>

    <meta-data
        android:name="android.appwidget.provider"
        android:resource="@xml/appwidget_info" />
    </receiver>

    <receiver android:name=".presentation.receiver.NotificationActionReceiver"/>

    <service android:name=".data.service.ListenerService" />
</application>

appwidget_provider

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
     android:initialKeyguardLayout="@layout/app_widget_4"
     android:initialLayout="@layout/app_widget_4"
     android:minHeight="110dp"
     android:minWidth="250dp"
     android:updatePeriodMillis="0"
     android:resizeMode="horizontal"
     android:widgetCategory="home_screen"
     android:previewImage="@mipmap/ic_launcher"
  android:configure="com.########.presentation.activity.ConfigActivity"/>

Any help would be gladly appreciate

EDIT If I remove the config activity from the android:configure attribute of the apwidget-provider, appwidget does appear on the home screen. It let me think that the problem is in the intent sent in the result of my config activity


Solution

  • You are passing an array of widgets IDs. Try to change your code to:

    In onCreate add

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

    then do:

    private void saveConfig() {
    
        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
        ConfigActivity.updateAppWidget(context, appWidgetManager, mAppWidgetId);
    
        Intent updateIntent = new Intent();
        updateIntent .putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
        setResult(RESULT_OK, updateIntent);
    
        finish();
    }