I'm developing and Android widget app using kotlin. I want my app can pin widget to home screen programmatically. As the android docs said, I can use requestPinAppWidget() to achieve this. There is a parameter which is an pending intent used as a callback. Normally system will notify app that widget was pinned successfully by this callback. This worked fine on Samsung, Oppo, etc .. but not on Xiaomi device (like Redmi).
This is my code
val pinnedWidgetCallbackIntent = Intent(Intent.ACTION_MAIN)
pinnedWidgetCallbackIntent.addCategory(Intent.CATEGORY_HOME)
pinnedWidgetCallbackIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
val successCallback = PendingIntent.getActivity(
context,
0,
pinnedWidgetCallbackIntent,
PendingIntent.FLAG_UPDATE_CURRENT
)
appWidgetManager.requestPinAppWidget(myProvider, null, successCallback)
When I call requestPinAppWidget, system should launch the pin widget flow. And after the widget is pinned successfully, it will open the home screen to show the widget had just pinned. But on xiaomi device, nothing happen.
Can anyone help me to figure out what happen on xiaomi device and how can I fix it.