I want to keep the value used by widget in a variable.
Even if it is counted up with onReceive in the following source, onReceive is next
When called, the counter value returns to 0.
How can I keep the value?

Thanks for your consideration.

Applicable source code
class NewAppWidget: AppWidgetProvider () {
    override fun onUpdate (context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
        // There may be multiple widgets active, so update all of them
        for (appWidgetId in appWidgetIds) {
            updateAppWidget (context, appWidgetManager, appWidgetId)
    var counter = 0 // I want to count up this value
    override fun onReceive (context: Context ?, intent: Intent?) {
        super.onReceive (context, intent)
        Log.i ("Log", counter.toString ()) // 0 is output no matter how many times it is called
        counter + = 1
    override fun onEnabled (context: Context) {
        // Enter relevant functionality for when the first widget is created
    override fun onDisabled (context: Context) {
        // Enter relevant functionality for when the last widget is disabled
internal fun updateAppWidget (context: Context, appWidgetManager: AppWidgetManager, appWidgetId: Int) {
    val intent = Intent (context, NewAppWidget :: class.java)
    // Construct the RemoteViews object
    val views = RemoteViews (context.packageName, R.layout.new_app_widget)
    // Instruct the widget manager to update the widget
    val pIntent = PendingIntent.getBroadcast (context, appWidgetId, intent, 0)
    views.setOnClickPendingIntent (R.id.appwidget_text, pIntent)
    appWidgetManager.updateAppWidget (appWidgetId, views)
Supplemental information (FW/tool version etc.)

API 26