Widgets Don't Respond When Re-Added Through Code

Widgets don't respond when re-added through code

We can invoke a dialog asking user to permit our app to be able to bind widget IDs with the following code:

Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, sSavedWidgetId);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, mWidgetInfo.provider);
startActivityForResult(intent, REQUEST_BIND_WIDGET);

And bind widget IDs to make them actually work with:

Bundle opts = new Bundle();
opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH, maxWidth);
opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT, minHeight);
opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH, maxWidth);
opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT, maxHeight);

widgetManager.bindAppWidgetIdIfAllowed(widgetId, mWidgetInfo, opts);

For more information refer:

  • AppWidgetManager.ACTION_APPWIDGET_BIND
  • AppWidgetManager.bindAppWidgetIdIfAllowed()

I wish someone would have answered the same 5 years ago.

Android widget not responding to touches

Actually you have answered your question. But let's clarify some things:

A. AppWidgets are NOT killed as long as they are on a home screen. But they don't belong to you. They are running in the process of the home application. To be more specific their views are running in the process of the home application, this is why you are see your widget but it doesn't do what you are expected and this is why we are using RemoteViews instead of Views in order to update them.

B. AppWidgetProviders (in your case the WordWidget class), on the other hand, are destroyed as soon as the onReceive method finishes. All the variables in them are re-initialized every time the onReceive method gets called. This is why your number never increases. The purpose of an AppWidgetProvider is to update the widget's RemoteViews and to inform your application that a registered broadcast has arrived.

C. AppWidgetProvider's onUpdate method provides you an Array with the widgets Ids that must be updated. This is the only code point you can use to get the number of your widget instances and their Ids. Because of the RemoteViews there is NO way to get some useful value from the Views of your widget (for example you can NOT read the counter value from the TextView) so you must use the provided information and DO persist your counter values per widget id. When the next onUpdate gets called you read the value from the storage, increase it, update the widget with the new value and then store the new value back.

D. If your widget has to do many things or slow things (like networking) when its time to update itself, you should consider using a service for this. But in your case (to increase a number) your approach is just fine as long as you persist the counters in the persistent storage or somewhere else.

Finally I 've noticed that in your onReceive override you are calling the "super.onReceive" only if you don't receive the "UPDATE_NUMBER" action. This is NOT a good practice, unless there is a GOOD reason (that's another story) always call super.onReceive as the first or the last command in your override.

Hope this helps...

Python Kivy self.add_widget() doesn't update while code running

Your code:

Chat_history_update().chat_history(request)

is creating a new instance of Chat_history_update, and calling chat_history() for that new instance. That new instance is not part of your GUI, so you will see no effect. The fix is to access the correct instance of Chat_history_update (the one that is in your GUI). To do that, you can add an id in your kv:

        ScrollView: #<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Here my text display
Chat_history_update:
id: chu
orientation: "vertical"
size_hint: 1, None
height: self.minimum_height

And then use that id in your py code:

def on_text_validate(self,widget): #<<<<<<<<<<<<<<<<<<<<<<<<<<< input text
request=widget.text
self.ids.chu.chat_history(request)

Flutter ListView not working properly when adding widgets to List

You can copy paste run full code below

You can use ListView.builder

For demo, I add Widget2 with different icon

code snippet

ListView.builder(
scrollDirection: Axis.vertical,
itemCount: childrenAdded.length,
itemBuilder: (BuildContext context, int index) {
return childrenAdded[index];
})

working demo

Sample Image

full code

import 'dart:core';
import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}

class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
List<Widget> childrenAdded = [];
int count = 0;
@override
Widget build(BuildContext context) {
List<Widget> childrenGenerated = List.generate(count, (int i) => Widget1());
print("childrenGenerated:$childrenGenerated");
print("childrenAdded:$childrenAdded");
print("----------------");
return Scaffold(
appBar: AppBar(title: Text('some title')),
body: Column(
children: <Widget>[
Expanded(
child: ListView(
children: childrenGenerated,
scrollDirection: Axis.vertical,
),
),
Expanded(
child: ListView.builder(
scrollDirection: Axis.vertical,
itemCount: childrenAdded.length,
itemBuilder: (BuildContext context, int index) {
return childrenAdded[index];
}),
),
],
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed: () {
setState(() {
count++;
childrenAdded.add(
Widget2(),
);
});
},
),
);
}
}

class Widget1 extends StatelessWidget {
const Widget1({Key key}) : super(key: key);

@override
Widget build(BuildContext context) {
print("entered widget");
return Icon(
Icons.image,
size: 50,
);
}
}

class Widget2 extends StatelessWidget {
const Widget2({Key key}) : super(key: key);

@override
Widget build(BuildContext context) {
print("entered widget");
return Icon(
Icons.ac_unit,
size: 50,
);
}
}


Related Topics



Leave a reply



Submit