(AppLovin) App crashes when closing viewed video.

So when I click close on the ad what you see after you have viewed video the app crashes. Before crash app goes to next scene, but after half a second crash occurs. There is nothing unusual in console log. 

Here is my ad_screen.lua:

[lua]

local composer = require( “composer” )

local scene = composer.newScene()

local applovin = require( “plugin.applovin” )

local isIncentivized = false

local displayed = false

local currency

                

local function changeScene(str,text)

    local scenename = str or “title_screen”

    local options = {

                    effect  =   “fade”,

                    time    =   “800”,

                    params = {currency = text}

                }

    composer.gotoScene( scenename,options )

end


– Scene event functions


– create()

function scene:create( event )

    local sceneGroup = self.view

    – Code here runs when the scene is first created but has not yet appeared on screen

    local function adListener( event )

        if ( event.phase == “init” ) then  – Successful initialization

            print( event.isError )

            

        elseif ( event.phase == “loaded”) then  – The ad was successfully loaded

            print( event.type )

            – Show the ad

            applovin.show(isIncentivized)

        elseif ( event.phase == “failed” ) then  – The ad failed to load

            print( event.type )

            print( event.isError )

            print( event.response )

            

            changeScene(“title_screen”)

        elseif ( event.phase == “displayed” or event.phase == “playbackBegan” ) then  – The ad was displayed/played

            displayed = true

            

        elseif ( event.phase == “hidden” or event.phase == “playbackEnded” ) then  – The ad was closed/hidden

            

            local scenename = “title_screen”

            if displayed then scenename = “chooseLevel” end

            changeScene(scenename,currency)

            

        elseif ( event.phase == “clicked” ) then  – The ad was clicked/tapped

            print( event.type )

        end

    end

    

    applovin.init( adListener, { sdkKey=“8ALNXJgeXthX8FbRtB1RH4KvvURLTgFcra-Nue0QViydzT4rk6G0Bbv45LPZ0eArE8C7nDTHxUwu1_im5-nGZf”, verboseLogging=true } )

end

– show()

function scene:show( event )

    local sceneGroup = self.view

    

    local phase = event.phase

    

– Initialize the AppLovin plugin

    if ( phase == “will” ) then

        – Code here runs when the scene is still off screen (but is about to come on screen)

        displayed = false

        currency = “”

        

        if event.params then

            currency = event.params.currency

        end

        

        applovin.load(isIncentivized)

    elseif ( phase == “did” ) then

        – Code here runs when the scene is entirely on screen

    end

end

– hide()

function scene:hide( event )

    local sceneGroup = self.view

    local phase = event.phase

    if ( phase == “will” ) then

        – Code here runs when the scene is on screen (but is about to go off screen)

    elseif ( phase == “did” ) then

        – Code here runs immediately after the scene goes entirely off screen

    end

end

– destroy()

function scene:destroy( event )

    local sceneGroup = self.view

    – Code here runs prior to the removal of scene’s view

end


– Scene event function listeners


scene:addEventListener( “create”, scene )

scene:addEventListener( “show”, scene )

scene:addEventListener( “hide”, scene )

scene:addEventListener( “destroy”, scene )


return scene

[/lua]

I decided to try not to use scene and no crashing anymore. I don’t know why I decided to do this with scenes :smiley:

Because of the way Composer creates scenes and enters and exits them, it’s almost never good advice to do one-time initialization things. For instance, in your code above, you are initializing the ad provider in scene:create(). What happens when you leave that scene and destroy it? Now you’ve got a listener running and no function for it to go to. Or the second scenerio, you return and re-initialize it.

Move your listener function and applovin.init() call to main.lua. It’s okay to load the ad where you are doing it.

I decided to try not to use scene and no crashing anymore. I don’t know why I decided to do this with scenes :smiley:

Because of the way Composer creates scenes and enters and exits them, it’s almost never good advice to do one-time initialization things. For instance, in your code above, you are initializing the ad provider in scene:create(). What happens when you leave that scene and destroy it? Now you’ve got a listener running and no function for it to go to. Or the second scenerio, you return and re-initialize it.

Move your listener function and applovin.init() call to main.lua. It’s okay to load the ad where you are doing it.