Android Question [Solved]FirstTime is not reset when activity opened a second time

James Chamblin

Active Member
Licensed User
Longtime User
I have two activities. The user will select a file from activity A, then activity B is started with StartActivity(). The file is loaded and processed in Activity B. Not wanting to reload and reprocess the file every time the orientation is changed, I check for FirstTime flag and only load when it is true.

Problem comes when the user exits the second activity with the back arrow and selects a new file from activity A. The FirstTime flag remains false so the new file doesn't get loaded and processed. The example below shows the problem, using strings instead of files to illustrate.

I tried putting If UserClosed = True Then FirstTime = True but that didn't do anything. Anyway to reset the FirstTime flag?
 

Attachments

  • example.zip
    8.9 KB · Views: 140

James Chamblin

Active Member
Licensed User
Longtime User
This is exactly how it is supposed to behave. FirstTime is only true when the process is created.

Android Process and activities life cycle
The question is, is there a way to reset the FirstTime flag when the Activity is UserClosed? For the moment, I have com up with this little cludge.
B4X:
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
  
   Dim ActivityRun As Boolean = False 'Flag to test if activity is still running

End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.

End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   Activity.LoadLayout("b")
   If ActivityRun = False Then 'If the Activity was just called by another, then we will process some data
     ActivityRun = True 'Set the flag to True
     DoInitialProcessing
   End If
   
End Sub

Sub Activity_Pause (UserClosed As Boolean)
   If UserClosed = True Then ActivityRun = False 'If the user presses the back button, set ActivityRun to false

End Sub
 
Upvote 0

Todd Carlton

Member
Licensed User
Longtime User
As a work-around, you can write a temporary file to the File.DirInternalCache directory and on Activity B's Resume, not re-process the user-selected file if the 'flag file' is present. Then, in the Resume for Activity "A", check for the flag file and delete it if it's present.

-OCS
 
Upvote 0

James Chamblin

Active Member
Licensed User
Longtime User
What do you do in DoInitialProcessing?
In my example above, I am just using DoInitialProcessing as shorthand for everything I need to set up, instead of copying the entire thing. In my app, DoInitialProcessing is replaced with loading a script file, reading the header to set initial values and set up other things. Noting too time consuming, but shouldn't be done every time there is a change in orientation as some values will change over time and would be reset to initial values if the file is processed again.

Thinking about it for a while, I realize I could create a separate class module that would take care of file processing before calling the second activity. That would eliminate the need to check FirstTime all together.

Since it seems impossible to reset the FirstTime flag, and three different alternatives have been presented, I will go ahead and mark this solved.
 
Upvote 0
Top