Android Question Activity Restarts after YouTubeLibrary fires

Shahid Saeed

Active Member
Licensed User
Longtime User
I am using a splash screen, and app is setup for Full Screen portrait mode, when you play a video it opens in landscape full screen and when quit video the Activity restarts and this time it stays at splash screen.

I have tried to handle through pause sub, resume sub but no luck; Here is the code for your reference:

B4X:
Sub Activity_Create(FirstTime As Boolean)
   
    pnllist.Initialize
    BayanCList.Initialize
   
    If FirstTime Then  
    isFirst = True  
    End If
   
    SplashTimer.Initialize("SplashTimer", 4000)
    SplashTimer.Enabled = True
    Activity.LoadLayout("spalsh")
   
End Sub
Sub Activity_Resume  
    If isFirst = False Then  
        Dim pnl As Panel = BayanList.GetPanel(posBayan-1)
        If pnllist.Size > 0 Then
        For i = 0 To pnllist.Size-1
            Dim p As Panel = pnllist.Get(i)
            p.Color = Colors.Transparent
        Next
        End If
        pnl.Color = pnlBG      
    End If  
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    If UserClosed = True Then
    Activity.Finish
    End If
    posBayan = posBayan
    isFirst = False    
End Sub

Sub SplashTimer_Tick  
    SplashTimer.Enabled = False
    DoEvents
    Activity.RemoveAllViews
    DoEvents
    Activity.LoadLayout("Main")
End Sub
 

Shahid Saeed

Active Member
Licensed User
Longtime User
The activity will be recreated after the orientation change.
1. You should move all the layout code to Activity_Create.
2. Remove the DoEvents from SplashTimer_Tick
3. If the splash screen has already been displayed then you need to immediately load the main layou.

All done as you said but still the activity is being re-created?

B4X:
Sub Activity_Create(FirstTime As Boolean)
   
    pnllist.Initialize
    BayanCList.Initialize
   
    If FirstTime Then  
    isFirst = True  
    End If
   
    SplashTimer.Initialize("SplashTimer", 4000)
    SplashTimer.Enabled = True
    Activity.LoadLayout("Main")
    Activity.LoadLayout("spalsh")
    
   
End Sub
Sub Activity_Resume  
    If isFirst = False Then  
        Dim pnl As Panel = BayanList.GetPanel(posBayan-1)
        If pnllist.Size > 0 Then
        For i = 0 To pnllist.Size-1
            Dim p As Panel = pnllist.Get(i)
            p.Color = Colors.Transparent
        Next
        End If
        pnl.Color = pnlBG      
    End If  
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    If UserClosed = True Then
    Activity.Finish
    End If
    posBayan = posBayan
    isFirst = False    
End Sub

Sub SplashTimer_Tick  
    SplashTimer.Enabled = False
    Activity.RemoveViewAt(2)    
End Sub
 
Upvote 0

Shahid Saeed

Active Member
Licensed User
Longtime User
It is recreated because you are calling Activity.Finish in Activity_Pause. This call destroys the activity.

I am calling Activity.Finish only if userClosed = True.

B4X:
Sub Activity_Pause (UserClosed AsBoolean)
If UserClosed = TrueThen
Activity.Finish
EndIf
posBayan = posBayan
isFirst = False End Sub

After intensive search on Google Finally I got the solution: I just added the following code to Mainifest file and activity is not being re-created now

B4X:
SetActivityAttribute(Main,android:configChanges, "orientation|screenSize")

The above code just did the magic and it can be applied to any activity which user doesn't want android to interrupt it.
 
Upvote 0

Shahid Saeed

Active Member
Licensed User
Longtime User
You do not need to set this attribute. In the long run it will only cause problems.

The activity is recreated when the orientation changes. This is how Android works. You need to build the Activity code correctly to deal with the activity being destroyed and recreated.

Even after removing Activity.Finish from Activity_pause The Activity is being re-created. The Attributes code i have provided is only preventing android to change anything in the activity which is exactly what I wanted and If we should not use it and go with the activity code correctly then what could be the correct code?
 
Upvote 0

Shahid Saeed

Active Member
Licensed User
Longtime User
I just looked into android developer guide and the following links also states that the attribute is a standard behavior within the Android framework:

http://developer.android.com/guide/topics/manifest/activity-element.html

B4X:
<activityandroid:allowTaskReparenting=["true" | "false"]
          android:alwaysRetainTaskState=["true" | "false"]
          android:clearTaskOnLaunch=["true" | "false"]
          android:configChanges=["mcc", "mnc", "locale",
                                 "touchscreen", "keyboard", "keyboardHidden",
                                 "navigation", "screenLayout", "fontScale", "uiMode",
                                 "orientation", "screenSize", "smallestScreenSize"]
          android:enabled=["true" | "false"]
          android:excludeFromRecents=["true" | "false"]
          android:exported=["true" | "false"]
          android:finishOnTaskLaunch=["true" | "false"]
          android:hardwareAccelerated=["true" | "false"]
          android:icon="drawable resource"
          android:label="string resource"
          android:launchMode=["multiple" | "singleTop" |
                              "singleTask" | "singleInstance"]
          android:multiprocess=["true" | "false"]
          android:name="string"
          android:noHistory=["true" | "false"]  
          android:parentActivityName="string" 
          android:permission="string"
          android:process="string"
          android:screenOrientation=["unspecified" | "behind" |
                                     "landscape" | "portrait" |
                                     "reverseLandscape" | "reversePortrait" |
                                     "sensorLandscape" | "sensorPortrait" |
                                     "userLandscape" | "userPortrait" |
                                     "sensor" | "fullSensor" | "nosensor" |
                                     "user" | "fullUser" | "locked"]
          android:stateNotNeeded=["true" | "false"]
          android:taskAffinity="string"
          android:theme="resource or theme"
          android:uiOptions=["none" | "splitActionBarWhenNarrow"]
          android:windowSoftInputMode=["stateUnspecified",
                                       "stateUnchanged", "stateHidden",
                                       "stateAlwaysHidden", "stateVisible",
                                       "stateAlwaysVisible", "adjustUnspecified",
                                       "adjustResize", "adjustPan"] >   
    . . .</activity>
 
Upvote 0

MaFu

Well-Known Member
Licensed User
Longtime User
Why not show the splash screen only if FirstTime is True?
Sorry, but if i see a splash screen for about 4 seconds every time i switch to a program, thats for me reason enough to uninstall it.
 
Upvote 0

Shahid Saeed

Active Member
Licensed User
Longtime User
But beware in this case your activity layout isn't recreated if you rotate your device.
Why not show the splash screen only if FirstTime is True?
Sorry, but if i see a splash screen for about 4 seconds every time i switch to a program, that for me reason enough to uninstall it.

Even If first time is Flase when the Activity re-creates the First Time will become True?

The application is running in Full Screen Portrait Mode. The only problem is through YouTube Plugin when you play a video it goes Full Screen Landscape which is good though, but when you leave the video; Main Acitivity is being re-created. I am using Custom List View to display almost 100+ Rows. Now if a user was watching video and number 50 and when he leave video and come back to main activity which is already re-started and showing him at row #1 again. But without letting the Activity being re-created which the Mainifest Attrbutes code does App is working as it should be when you leave the video the selected panel is 50 not #1
 
Upvote 0

MaFu

Well-Known Member
Licensed User
Longtime User
Even If first time is Flase when the Activity re-creates the First Time will become True?
No. FirstTime is only True if program starts, on activity recreate it's False.
 
Upvote 0

Shahid Saeed

Active Member
Licensed User
Longtime User
If this is how it works, than why I see the splash screen every-time and Why it doesn't show me the Correct position which I am defining in Activity_pause as well?
 
Upvote 0

MaFu

Well-Known Member
Licensed User
Longtime User
Look at your source:
B4X:
Sub Activity_Create(FirstTime As Boolean)
 
    pnllist.Initialize
    BayanCList.Initialize
 
    If FirstTime Then 
    isFirst = True 
    End If
 
    SplashTimer.Initialize("SplashTimer", 4000)
    SplashTimer.Enabled = True
    Activity.LoadLayout("Main")
    Activity.LoadLayout("spalsh")
   
 
End Sub
You load the splash layout every time independend of the FirstTime value.
Try this:
B4X:
Sub Activity_Create(FirstTime As Boolean)
 
    pnllist.Initialize
    BayanCList.Initialize
 
    Activity.LoadLayout("Main")
    If FirstTime Then  
        isFirst = True  
        SplashTimer.Initialize("SplashTimer", 4000)
        SplashTimer.Enabled = True
        Activity.LoadLayout("spalsh")
    End If
   
End Sub
 
Upvote 0

Shahid Saeed

Active Member
Licensed User
Longtime User
Look at your source:
B4X:
Sub Activity_Create(FirstTime As Boolean)

    pnllist.Initialize
    BayanCList.Initialize

    If FirstTime Then
    isFirst = True
    End If

    SplashTimer.Initialize("SplashTimer", 4000)
    SplashTimer.Enabled = True
    Activity.LoadLayout("Main")
    Activity.LoadLayout("spalsh")
 

End Sub
You load the splash layout every time independend of the FirstTime value.
Try this:
B4X:
Sub Activity_Create(FirstTime As Boolean)

    pnllist.Initialize
    BayanCList.Initialize

    Activity.LoadLayout("Main")
    If FirstTime Then
        isFirst = True
        SplashTimer.Initialize("SplashTimer", 4000)
        SplashTimer.Enabled = True
        Activity.LoadLayout("spalsh")
    End If
 
End Sub

OK this way it doesn't show splash screen after leaving the video; but when activity is recreated it doesn't have the panel position. Since it is not pausing the APP I guess it doesn't know the position as well and The selected panel is always panel 1; So here how can I have the panel position?
 
Upvote 0

MaFu

Well-Known Member
Licensed User
Longtime User
The standard android workflow is to save the current activity state on Activity_Pause and restore it on Activity_Resume.
A simple way to save/restore values is using the KeyValueStore class.
 
Upvote 0

Shahid Saeed

Active Member
Licensed User
Longtime User
This is the Debug Log of Whole Work Flow:
B4X:
Installing file.
PackageAdded: package:com.sidso.bayan
** Activity (main) Create, isFirst = true **   'this is first time when app starts
** Activity (main) Resume ** 'It shows the splash screen
** Activity (main) Pause, UserClosed = false ** 'I don't know why it pauses the app
** Activity (main) Resume ** 'Again resumes without splash screen
** Activity (main) Pause, UserClosed = false ** 'Youtube Video Pressed
** Activity (main) Create, isFirst = false ** 'Youtube Video Started
** Activity (main) Resume ** 'Video Loaded
sending message to waiting queue (OnActivityResult)
** Activity (main) Create, isFirst = false ** 'Video Left & Activity Re-Created
running waiting messages (1)
** Activity (main) Resume ** 'Activity Re-created but no values

I am using the following code to store and retrieve values in Pause/Resume Activities:

B4X:
Sub Activity_Resume   
    If isFirst = False Then   
        Dim pnl As Panel = BayanList.GetPanel(posBayan-1)
        If pnllist.Size > 0 Then
        For i = 0 To pnllist.Size-1
            Dim p As Panel = pnllist.Get(i)
            p.Color = Colors.Transparent
        Next
        End If
        pnl.Color = pnlBG
        'Log(posBayan)
    End If   
    'Log(posBayan)
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    If UserClosed = True Then
    Activity.Finish
    End If
    posBayan = posBayan
    isFirst = False
    'Log(posBayan)
End Sub

But It doesn't seems to be working
 
Upvote 0

Shahid Saeed

Active Member
Licensed User
Longtime User
By default posBayan value is 1 but when we press any panel it changes it to that panel value. So I am using posBayan = PosBayan to save the Current selected panel value. What doesn't make sense in that?

B4X:
Sub BayanList_ItemClick (Position As Int, Value As Object)
    Dim pnl As Panel = Value
    If pnllist.Size > 0 Then
        For i = 0 To pnllist.Size-1
            Dim p As Panel = pnllist.Get(i)
            p.Color = Colors.Transparent
        Next
    End If
    pnl.Color = pnlBG
    posBayan = pnl.Tag
    BayanCode = BayanCList.Get(Position)
    YouTube(Position)   
End Sub
 
Upvote 0
Top