Android Question Dialogs -> R8Optimizer & B4a 14.2

ilan

Expert
Licensed User
Longtime User
hi

after updating all my apps i get many many complains about dialogs that opens and close immediately and people cannot use my apps any more. i know that i should switch to b4xdialogs but:

1. to much effort now to implement in all dialogs
2. ugly i like much more the native time, date pickers (wheel)
3. why does it works super fine on my phone but on others it closes after dialog shows?

is there a quick workaround to make it work ?

just an example of use:
Date Dialog:
Code: Copy
        Sleep(50)
            Dim dd As DateDialog
            dd.SetDate(DateTime.GetDayOfMonth(DateTime.Now),DateTime.GetMonth(DateTime.Now),DateTime.GetYear(DateTime.Now))
            dd.ShowCalendar = False
            Dim sf As Object = dd.ShowAsync("","","אישור","ביטול","", Null, False)
            Wait For (sf) Dialog_Result(Result As Int)
            If Result = DialogResponse.POSITIVE Then
                addSpecialDays(dd.DateTicks,1,0)
            End If


TimeDialog:
Code: Copy
Dim td As TimeDialog
    td.Is24Hours = True
    If datepnltimelbl.Tag <> "" And IsNumber(datepnltimelbl.Tag) Then
        Dim timeIn As Long = datepnltimelbl.Tag
        If timeIn > 0 Then
            td.TimeTicks = timeIn
        Else
            td.TimeTicks = DateTime.Now
        End If
    Else
        td.TimeTicks = DateTime.Now
    End If
    Dim sf As Object = td.ShowAsync("", "שעת כניסה", "אישור", "", "ביטול", Null, False)
    Wait For (sf) Dialog_Result(Result As Int)
    If Result = DialogResponse.POSITIVE Then
        datepnltimelbl.Text = DateTime.Time(td.TimeTicks)
        datepnltimelbl.Tag = td.TimeTicks
    End If

inputdialog:
Code: Copy
    Dim id As InputDialog
    id.InputType = id.INPUT_TYPE_TEXT
    Dim sf As Object = id.ShowAsync("", "נא להכניס הערה.", "אישור", "", "ביטול", Null, False)
    Wait For (sf) Dialog_Result(Result As Int)
    If Result = DialogResponse.POSITIVE Then
        shiftpnlcomments.Text = id.Input
    End If

i am loosing many users since the last updates so any suggestions is very much appreciated.

thank you
 
I'm not familiar with issues related to the system dialogs and R8. It is possible that there are, and they are solvable.

Did you see any crash report?
this is the weird part. there is no crash after the last update(s) in the last 2-3 days on my 2 phones everything works perfect but many users complain that the dialog show for 1 seconds and close they are not able to interact with it. no crash no crash reports it just closes.

they send me videos and i can see it they start interact and it closes like the activity is loosing focus on it or like back key is pressed and that forces the dialog to close.

gemini:

he immediate cause of dialogs opening and closing instantly on newer Android versions (or after recompiling with recent Android Target SDKs like 31+) when using legacy Dialogs library components (DateDialog, TimeDialog, InputDialog) is an Activity Context issue combined with window focus / lifecycle events during target SDK transitions.

When Target SDK is updated, Android enforces strict window focus and theme rules on legacy modal dialogs. If the underlying Activity loses focus even momentarily, or if the dialog is bound to a context that gets recreated or shifted during ShowAsync, Android automatically dismisses legacy AlertDialog instances instantly with DialogResponse.CANCEL.

Why it works on your test device but fails on users' phones comes down to device-specific Android themes / window animation settings: devices with custom OEM skins (Samsung One UI, Xiaomi MIUI, etc.) or different default dialog themes trigger immediate window focus transitions upon dialog display, dismissing legacy dialogs before the user can interact.

yesterday gemini said it has something todo with the IME implementation. (non b4xpages)
so it told me to update the manifest:

SetActivityAttribute(Main, android:windowSoftInputMode, "stateAlwaysHidden|adjustNothing")


the biggest issue is that i am not able to reproduce it but i see videos users send me and the revues:

אחלה אפליקצייה משתמש שנים. אחרי העדכון האחרון, משהו רע קרה. הכל תקוע, קופץ ולא עובד מבאס ממש.

יש תקלה באפליקציה שבה אני מזין שעות או טקסט והתיבה נעלמת

היישומון האפליקציה הפסיק לעבוד לא נותן להכניס משמרת חדשה תתקנו את זה היישומון האפליקציה הפסיק לעבוד אי אפשר להכניס משמרת חדשה תתקנו את זה
 
Upvote 0
Publish an app without r8 as a temporal solution. See if it actually solves the issue or maybe something else changed. I'm not sure at all that r8 is related to such behavior.
maybe its b4a 14.2?

the thing is that without R8 i am getting warnings in google play console.

i will try to switch to b4x dialogs the problem i could not find a nice time and date picker. similar to native dialogs.
 
Upvote 0
ok some updates. i removed the dialogs (v4.01) library from the library manager and instead i added a class that loads the native dialogs using JavaObject

Code: Copy
' Class Module: NativeDialogs
Sub Class_Globals
    Private xui As XUI
    Public mTargetDialog As B4XDialog
End Sub

Public Sub Initialize (TargetB4XDialog As B4XDialog)
    mTargetDialog = TargetB4XDialog
End Sub

' Native Time Wheel Dialog
Public Sub ShowTimeDialog(Title As String, InitialTicks As Long) As ResumableSub
    Dim hour As Int = DateTime.GetHour(InitialTicks)
    Dim minute As Int = DateTime.GetMinute(InitialTicks)
  
    Dim ctxt As JavaObject
    ctxt.InitializeContext
  
    ' Fetch HoloWheelTheme from Manifest
    Dim res As JavaObject = ctxt.RunMethod("getResources", Null)
    Dim pkgName As String = ctxt.RunMethod("getPackageName", Null)
    Dim themeResId As Int = res.RunMethod("getIdentifier", Array("HoloWheelTheme", "style", pkgName))
  
    If themeResId = 0 Then themeResId = 16973931 ' Fallback to Theme_Holo_Light_Dialog
  
    Dim themeWrapper As JavaObject
    themeWrapper.InitializeNewInstance("android.view.ContextThemeWrapper", Array(ctxt, themeResId))
  
    Dim OnTimeSetListener As Object = ctxt.CreateEventFromUI("android.app.TimePickerDialog$OnTimeSetListener", "OnNativeTimeSet", Null)
  
    Dim tpd As JavaObject
    tpd.InitializeNewInstance("android.app.TimePickerDialog", Array(themeWrapper, OnTimeSetListener, hour, minute, True))
  
    If Title <> "" Then tpd.RunMethod("setTitle", Array(Title))
  
    tpd.RunMethod("show", Null)
  
    Wait For OnNativeTimeSet_Event (MethodName As String, Args() As Object)
  
    If Args <> Null And Args.Length >= 3 Then
        Dim selHour As Int = Args(1)
        Dim selMin As Int = Args(2)
      
        Dim todayDateTicks As Long = DateTime.DateParse(DateTime.Date(DateTime.Now))
        Dim hoursTicks As Long = selHour * DateTime.TicksPerHour
        Dim minutesTicks As Long = selMin * DateTime.TicksPerMinute
      
        Return todayDateTicks + hoursTicks + minutesTicks
    Else
        Dim cancelReturn As Long = -1
        Return cancelReturn
    End If
End Sub

' Native Date Wheel Dialog
Public Sub ShowDateDialog(InitialTicks As Long) As ResumableSub
    Dim c As JavaObject
    c = c.InitializeStatic("java.util.Calendar").RunMethod("getInstance", Null)
    c.RunMethod("setTimeInMillis", Array(InitialTicks))
  
    Dim year As Int = c.RunMethod("get", Array(1))
    Dim month As Int = c.RunMethod("get", Array(2))
    Dim day As Int = c.RunMethod("get", Array(5))
  
    Dim ctxt As JavaObject
    ctxt.InitializeContext
  
    Dim res As JavaObject = ctxt.RunMethod("getResources", Null)
    Dim pkgName As String = ctxt.RunMethod("getPackageName", Null)
    Dim themeResId As Int = res.RunMethod("getIdentifier", Array("HoloWheelTheme", "style", pkgName))
  
    If themeResId = 0 Then themeResId = 16973931
  
    Dim themeWrapper As JavaObject
    themeWrapper.InitializeNewInstance("android.view.ContextThemeWrapper", Array(ctxt, themeResId))
  
    Dim OnDateSetListener As Object = ctxt.CreateEventFromUI("android.app.DatePickerDialog$OnDateSetListener", "OnNativeDateSet", Null)
  
    Dim dpd As JavaObject
    dpd.InitializeNewInstance("android.app.DatePickerDialog", Array(themeWrapper, OnDateSetListener, year, month, day))
  
    dpd.RunMethod("show", Null)
  
    Wait For OnNativeDateSet_Event (MethodName As String, Args() As Object)
  
    If Args <> Null And Args.Length >= 4 Then
        Dim selYear As Int = Args(1)
        Dim selMonth As Int = Args(2) + 1
        Dim selDay As Int = Args(3)
      
        DateTime.DateFormat = "dd/MM/yyyy"
        Dim dateStr As String = $"${NumberFormat(selDay, 2, 0)}/${NumberFormat(selMonth, 2, 0)}/${selYear}"$
        Return DateTime.DateParse(dateStr)
    Else
        Dim cancelReturn As Long = -1
        Return cancelReturn
    End If
End Sub

' B4XInputTemplate Dialog
Public Sub ShowInputDialog(Title As String, Prompt As String, InitialText As String, ConfirmText As String, CancelText As String, IsRTL As Boolean, IsNumeric As Boolean, AllowDecimals As Boolean) As ResumableSub
    If mTargetDialog.IsInitialized = False Then
        Log("Error: B4XDialog (mTargetDialog) was not initialized in NativeDialogs!")
        Return Null
    End If

    Sleep(50)
  
    Dim inputTemplate As B4XInputTemplate
    inputTemplate.Initialize
    inputTemplate.lblTitle.Text = Prompt
    inputTemplate.lblTitle.Font = xui.CreateDefaultFont(13)
    inputTemplate.Text = InitialText

    If IsNumeric Then
        inputTemplate.ConfigureForNumbers(AllowDecimals, False)
    End If
  
    If IsRTL Then
        inputTemplate.TextField1.SetTextAlignment("CENTER", "RIGHT")
        inputTemplate.lblTitle.SetTextAlignment("CENTER","RIGHT") 'HEBREW ONLY!!
    Else
        inputTemplate.TextField1.SetTextAlignment("CENTER", "LEFT")
        inputTemplate.lblTitle.SetTextAlignment("CENTER","LEFT") 'ENGLISH ONLY!!
    End If
  
    mTargetDialog.Title = Title
  
    Dim rs As ResumableSub = mTargetDialog.ShowTemplate(inputTemplate, ConfirmText, "", CancelText)
  
    Try
        If mTargetDialog.Base.IsInitialized Then
            Dim parentView As B4XView = mTargetDialog.Base.Parent
            If parentView.IsInitialized Then
                mTargetDialog.Base.Top = parentView.Height * 0.15
            End If
        End If
    Catch
        Log(LastException)
    End Try

    Wait For (rs) Complete (Result As Int)
  
    If Result = xui.DialogResponse_Positive Then
        Return inputTemplate.Text.Trim
    Else
        Return Null
    End If
End Sub

i use in manifest this to get the wheel picker:

Code: Copy
' ========================================================
' Dialogs
' ========================================================

CreateResource(values, styles.xml,
<resources>
    <style name="HoloWheelTheme" parent="android:Theme.Holo.Light.Dialog">
        <item name="android:timePickerMode">spinner</item>
        <item name="android:datePickerMode">spinner</item>
    </style>
</resources>
)

i uploaded the app with #R8Optimizer: True and users say the issue is solved. what do you think could be the issue in dialogs library?
 
Last edited:
Upvote 0
Back
Top