Android Tutorial Android Process and activities life cycle

Witold Giejsztowt

New Member
Licensed User
Hi,

New guy here.
In Main module I am declaring Process_Globals variable.
What happens when I re-declare the same name variable in different activity module Process_Globals Sub ?
 

Erel

Administrator
Staff member
Licensed User
Nothing special. You are creating two different variables.
 

MrKim

Well-Known Member
Licensed User
Along these lines here is where a little tutorial would be most helpful.
We have activity. code, class, and service modules
Who can access what? And When?
Who can run what? And when?
 

svk123

Member
Licensed User
Can a Activity be cloned or copied? Is it possible to dynamically create new activities (forms) from code?
 

Erel

Administrator
Staff member
Licensed User
No. Each activity must be declared in the manifest file. However you can implement most of the activity logic in a class and then reuse this class from multiple activities.
 

svk123

Member
Licensed User
No. Each activity must be declared in the manifest file. However you can implement most of the activity logic in a class and then reuse this class from multiple activities.
Can Views such as ScrollView, Panel, Buttons, EditTexts be declared in the class which can be reused from multiple activities?
 

ilan

Expert
Licensed User
i have a question, if i exit my actvity and in activity_pause i have activity.finish then the activity will create it self in the next start
but if i exit the activity and i dont want to destroy it and i use the back button

after i return to the activity i have only a blank screen, the layout is not loaded.
what should i do to prevent it

the thing is that i dont want to close the main activity with activity.finish because i am using the paramters in Process_Globals of the main activity

that means: in activity 2 i am changing parameters in Process_Globals of the main activity so main activity can not be finished

all other acitivies i am using activity.finish in the pause event but if i use the back key in the main and return to my app its sows only a black screen

why?
 

Erel

Administrator
Staff member
Licensed User
You shouldn't call Activity.Finish in Activity_Pause.

If the user presses on the back button then the activity is destroyed (unless you caught the back button event).

Activity.Finish has nothing to do with the process global variables.
 

ilan

Expert
Licensed User
ok i want to understand,

so activity.finish should be called from a button for example and in the pause event i am saving data or whatever i want to do before the activity will be finished

should i also finish the main avtivity if i want to go to other activities?

the problem is that i want to use parameters that are in the main activity and change them, can i do that also if the main activity is called with activity.finish??

and why afte ri press in main screen the back button and i exit my application when i return i get a blank screen??

if the activity will be destroyd with the back button it should recreate it self on the next start and load the layout file etc...
 

Erel

Administrator
Staff member
Licensed User
hould i also finish the main avtivity if i want to go to other activities?
Only if you want to remove it from the activities stack.

the problem is that i want to use parameters that are in the main activity and change them, can i do that also if the main activity is called with activity.finish??
If you are accessing process global variables then yes.

and why afte ri press in main screen the back button and i exit my application when i return i get a blank screen??
See this quiz: http://www.basic4ppc.com/android/forum/threads/quiz-12-find-the-bug-process-activities-life-cycle.37899/

It covers several common mistakes.
 

ilan

Expert
Licensed User
the problem is that if i exit my app via home button and return to it everything is ok
but via the back button and return the layout is not loaded and i see a black empty screen

is this a bug? why via the homebutton the app load the activity layout again but via the back button not

but i do to solve the problem is i caught the back button and call ExitApplication but is this the right way ?
 

ilan

Expert
Licensed User
no not only if firsttime is true.
could it be the phone? Samsung 2? because i can create a very simple app with a layout that has 1 button and it does the same
if i exit the app with the back button when it returns i see black screen

i will make a video when i get home and show you
 

corwin42

Expert
Licensed User
Would be better if you show us the code than a video on the problem. Without the code we can only guess what you have done wrong.
 

ilan

Expert
Licensed User
Would be better if you show us the code than a video on the problem. Without the code we can only guess what you have done wrong.
ok

this is the main activity (its only loading my logo and then start activity menu)

B4X:
#Region  Project Attributes
    #ApplicationLabel: Ninja Hop
    #VersionCode: 13
    #VersionName: 1.42
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: landscape
    #CanInstallToExternalStorage: False
    #AdditionalRes: C:\Android SDK\extras\google\google_play_services\libproject\google-play-services_lib\res, com.google.android.gms
#End Region

#Region  Activity Attributes
    #FullScreen: true
    #IncludeTitle: false
#End Region

'Activity module
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim t1 As Timer
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.
Dim img As ImageView
Dim a1 As Animation
Dim x As Int
Dim y As Int

End Sub

Sub Activity_Create(FirstTime As Boolean)
   
    If FirstTime = True Then
    img.Initialize ("img")
    img.Bitmap = LoadBitmap(File.DirAssets , "pro2.png")
    img.Gravity = Gravity.FILL
   
Activity.Color = Colors.White
   
    y = (Activity.Height / 2) - 38dip
    x = (Activity.Width / 2) - 100dip
   
    Activity.AddView (img, x,y, 200dip ,76dip )

   
    t1.Initialize ("t1", 2000)

    a1.InitializeAlpha("",0,1)
    a1.start(img)
    a1.Duration = 1500
    a1.RepeatCount = 0
    'a3.RepeatMode = a3.REPEAT_REVERSE
   
    t1.Enabled = True
   
    End If

End Sub

Sub Activity_KeyPress (KeyCode As Int) As Boolean 'Return True to consume the event

If KeyCode = KeyCodes.KEYCODE_BACK Then                                   
        Return True                                                         
    Else
        Return False
    End If

End Sub

Sub t1_tick
t1.Enabled = False
StartActivity(menu)
End Sub


Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
Activity.Finish
End Sub
activity menu i load the layout (not if firsttime = true )

and when i exit my app via the back button = black screen, Home button is ok only back button

 

ilan

Expert
Licensed User
The timer is never enabled when FirstTime is false.
this is what i want, the timer will start after 2 seconds and start activity menu (and will finish activity main)
so it should run only on firsttime

do you know why do i get the black screen? via the back button but home button is ok?
 
Last edited:

Erel

Administrator
Staff member
Licensed User
do you know why do i get the black screen?
Yes. You are not loading any layout when FirstTime is False (which will happen after you press on the back key).

Note that it is a mistake to call Activity.Finish in Activity_Pause (not related to the missing layout).
 
Top