Task in background looses values of variables when restarted

78 views
Skip to first unread message

francwalter

unread,
Jun 22, 2023, 7:06:10 AM6/22/23
to Tasker
Hallo

I have a Task which runs in the background. The Task is "protected" against closing through Android by a Notify, which permanently shows in the Notification Bar.

In this start Task I initialize some variables which I use in the main Scene.
For every variable I have a condition to set the value only IF UNSET (to protect the variables beeing reset).
In the properties of the Task I have set the Collision Handling to "Abort New Task".

In my main Scene of my Task I write now something in some Text Input Fields, close the Task to background again and wait (even several hours).
When I reopen now that main Scene again through the Notify (which does a Perform Task of the main Task) or by pressing the Task's Icon on my Homescreen, the text inputs are all still there, which is good.
BUT: the variables in the main Task are all reset to their initialized values!
As if the Collision Handling were set to "Abort Existing Task" (and the variables would be unset then and initialized again).
Or as if the Task is not aware of the previous Task and starts all over, but takes over the Scene with all its text inputs.
Or as if the first Task is not runing anymore after going to background, only the scene.
Or as if it were a bug ;)

How can I setup my Task to recognize that my variables are already set and should not be reset to their initialized values WIHTOUT using global values?

Thank
frank

Android 9.0 with Tasker (direct version) 6.2.6-beta

francwalter

unread,
Jun 22, 2023, 7:20:27 AM6/22/23
to Tasker

Here is a test scenario (without Notify, but shows the same issue), Description as Text. Collision Handling is set to "Abort New Task":

That is the Task:

Task: zTestTaskCollisionVars
   
    A1: Variable Set [
         Name: %test
         To: 0
         Do Maths: On
         Max Rounding Digits: 0
         Structure Output (JSON, etc): On ]
        If  [ %test !Set ]
   
    A2: Show Scene [
         Name: TestTaskCollisionVars
         Display As: Activity
         Horizontal Position: 100
         Vertical Position: 100
         Animation: System
         Show Exit Button: On
         Show Over Keyguard: On
         Continue Task Immediately: On
         Allow Outside Boundaries: On
         Blocking Overlay +: On
         Overlay +: On ]


And this is the Scene for it:

Scene: TestTaskCollisionVars
 P:702x1248 L:-1x-1

  Orientation: System
  Background Colour: #33FFFFFF
  Action Bar Style: System
  Title: TestTaskCollisionVars
  Subtitle:
  Icon: null
  Tab Labels:
 Events:
  Key: 1069

Element: Button1/Button
 Geometry:
  P:104,416 523x405 L:-1,-1 -1x-1
 Content:
  Label: show test
  Label Size: 16
  Label Width Scale %: 100
  Label Colour: #FF000000
  Font:
  Position: Centre
  Icon: null
 Events:
  Click: 1066

Element: Button2/Button
 Geometry:
  P:104,821 523x405 L:-1,-1 -1x-1
 Content:
  Label: set test = 2
  Label Size: 16
  Label Width Scale %: 100
  Label Colour: #FF000000
  Font:
  Position: Centre
  Icon: null
 Events:
  Click: 1068

Element: TextEdit1/TextEdit
 Geometry:
  P:29,204 647x148 L:-1,-1 -1x-1
 Content:
  Text:
  Text Size: 24
  Text Width Scale Percent: 100
  Text Colour: #FFFFFFFF
  Font:
  Position: Centre
  Input Type: Normal Text
  Maximum Characters: 1000


I run the Task "zTestTaskCollisionVars" which sets var test = 0 and opens the Scene "TestTaskCollisionVars".
Here I set (with the button) var test = 2. I can show with the other button the value of var test.
I write something into the Text Edit Field.
Now I press Home on my phone and run zTestTaskCollisionVars again. The text in the TextEdit Field is still there, but var test is 0 again!!!

aa6vh

unread,
Jun 22, 2023, 10:03:33 AM6/22/23
to Tasker
There appears to be some misunderstandings here. A notification does not "protect" an individual task, it protects the Tasker application itself. Your local varible "%test" is always erased when your task exits, and when the task starts again, a new local variable is created. That is the definition of how local variables are supposed to work. It appears that to accomplish what you want, you will need to use a Global Variable instead. What is your reason for not wanting to use a Global variable?

francwalter

unread,
Jun 27, 2023, 4:35:00 AM6/27/23
to Tasker

aa6vh schrieb am Donnerstag, 22. Juni 2023 um 16:03:33 UTC+2:
... What is your reason for not wanting to use a Global variable?


I have many variables in my Tasks, I want to reduce therefore the use of global variables as much as I can.
I have already some, but only if there is global use for it (used by different Tasks, e.g. the ID of my external SD-Card or the DynDNS-URL of my fritzbox router, or when I want to keep the variable over a reboot, e.g. variables as logs etc.).
And I heard it is good coding style to avoid global variables.

But now, knowing that I misunderstood it (I really thought that the Task is running in background) I think again over it. Maybe I need to make some exceptions :)
Thank you very much for clarification!

What I still dont understand is, why does the Scene keeps its variable values? The text in Textareas e.g. is still there after the Scene beeing in the background.

aa6vh

unread,
Jun 27, 2023, 9:55:39 AM6/27/23
to Tasker
Because a scene is still "Active", along with any scene variables, when in the background. It is just not visible. You use a "Destroy Scene" when you are completely done with the scene and want to free up memory.

francwalter

unread,
Jul 1, 2023, 8:07:32 AM7/1/23
to Tasker
aa6vh schrieb am Dienstag, 27. Juni 2023 um 15:55:39 UTC+2:
Because a scene is still "Active", along with any scene variables, when in the background. It is just not visible.

Thats it!  I just use the Test Element Action in my main Task, where I initialize all my Task's variables :)
I put an IF Action with myvar is unset and here I set it first with an empty value, then I  get the real value out of the Scene.
This works :)
Thank!
Reply all
Reply to author
Forward
0 new messages