Android Question Question about activity resume and views

brelto85

Active Member
Licensed User
Longtime User
Hi,

I have this scenario and would like to understand what is the best development methodology to implement it

In the activity Main i've a central button that after click it starts a foreground service that run the speechrecognizer to permit the simulated continuos voice recognition

When service starts, the button and other views in the activity Main changed the layout (visible = false, image view button, position ecc.. )

I also implemented a method that after click on the notification, starts the app and run the voice recognition standard

My problem is that if the screen is off or the activity is in paused, when i click on notification and starts the app, sometimes i get an error in some views randomly (label not initialized ecc...) depends if the activity has been completly initialized or no

Exist better methods that allow the wait complete of view's initializing when activity resume?
I try to use the Thread library i don't know if is a correct way to do that
 

brelto85

Active Member
Licensed User
Longtime User
B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    mFirstTime = FirstTime
      
    If FirstTime Then
        'Start splash activity that initialize some object (profile, TTS ecc..)
        StartActivity(ActivitySplash)
        'some initialization...
        mLanguage = Common.GetLanguageCode
        mVoiceRecognition.Initialize("VoiceRecognition")
        mVoiceRecognition.Language = mLanguage
    End If
    CallSubDelayed("", "Initialize")
End Sub

B4X:
Sub Initialize   
    'initialize the navigation drawer and toolbar and transition animation of button and label that must run after click on start button
    InitializeNavigationDrawer
    InitializeDrawerPanel
    InitializeTransition
  
    Common.SetTextShadow(LSpeedValue,1 ,1 ,1, Colors.Gray)
    Common.SetTextShadow(LSpeed,1 ,1 ,1, Colors.Gray)
  
    AdView1.Initialize2("Ad", "xxxx", AdView1.SIZE_SMART_BANNER)
    Common.SetTypeface(Activity, Typeface.LoadFromAssets("Roboto-Light.ttf"))
  
    mFunctionManager.Initialize(Me)
    'NB!!!!!!! I've notice that this line is slow and freezes for a few seconds the activity
    ThisActivity.InitializeAndBindWithNoEvent(Social.Providers)
End Sub

B4X:
Sub Activity_Resume  
    mTracker.Start
    If AdView1.IsInitialized Then AdView1.Resume

    If mLanguage <> Common.GetLanguageCode Then
        ToastMessageShow("change",False)
        mLanguage = Common.GetLanguageCode
        SetLanguageVoiceRecognition
        SetGUILanguage  
    End If
  
    If IsPaused(ServiceMain) Then
        Dim wPhone As Phone
        StateManager.SetSetting("ringer_mode", wPhone.GetRingerMode())
        StateManager.SetSetting("ringer_level", wPhone.GetVolume(wPhone.VOLUME_VOICE_CALL))
        StateManager.SetSetting("lvolume_music", wPhone.GetVolume(wPhone.VOLUME_MUSIC))
        StateManager.SaveSettings
    else
        SetLabelStatus(Common.GetTextByLan("In ascolto..."))
        CallSubDelayed("", "SetStopState")
        CallSubDelayed2("", "ChangePanelsSpeedVisibility", True)
    End If
End Sub

The SetStopState and ChangePanelsSpeedVisibility changed the view's layout (visible, position ecc..)
It would be appropriate to perform that methods and the initialization of the views in separate threads and wait to be ended
As i explained earlier, if i click on the notification, the app wake up, but sometimes crash in activity_resume
 
Last edited:
Upvote 0

brelto85

Active Member
Licensed User
Longtime User
ThisActivity is a SocialApi's method library that initialize the wrappers

Why remove CallSubDelayed?
CallSubDelayed wait until the activity is resumed?
 
Upvote 0

RandomCoder

Well-Known Member
Licensed User
Longtime User
CallSubDellayed is used to call a sub in an activity that might be currently paused. There is no need to use it in activity resume as in you're situation you're calling sub's in the activity that is being made active already.
 
Upvote 0

brelto85

Active Member
Licensed User
Longtime User
ok, but if i gets the error "Label was not initialized" when i set the text into a Activity_Resume because when the user touch the notification i must change the properties, how and when i change it?
 
Upvote 0
Top