Android Tutorial Two activities example

An improved version, based on CallSubDelayed is available here.
This example demonstrates how to work with more than one activity and how to pass information between the activities.
In this example we have two activities. One activity is the "main" activity. When the user presses a button a "list" activity will be displayed. The user will choose an item from the list and then return to the main activity:


It is recommended that you first read the Activities life cycle tutorial if you haven't read it before.

In order to add a new or existing activity to your project you should choose Project - Add New / Existing Module.

Variables declared in Sub Process_Globals are public variables and can be accessed from other activities. Therefore we will save the chosen value in such a variable.

When the user presses on the "choose item" button we open the second activity:
Sub Button1_Click
End Sub
When we open an activity the current one is first paused and then the target activity is resumed (and created if needed).
You can see it in the logs:


The second activity is pretty simple:
Sub Process_Globals
    Dim result As String
    result = ""
End Sub

Sub Globals
    Dim ListView1 As ListView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    For i = 1 To 100
        ListView1.AddSingleLine("Item #" & i)
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub ListView1_ItemClick (Position As Int, Value As Object)
    result = value 'store the value in the process global object.
    StartActivity(Main) 'show the main activity again
End Sub
When the user presses on an item we store the value in the 'result' variable and we return to the main activity.

The main activity Resume event will be raised. So in this event we check if 'result' variable is not empty and change the label's text.
Sub Activity_Resume
    If Activity2.result.Length > 0 Then
        Label1.Text = "You have chosen: " & Activity2.result
        Label1.Text = "Please press on the button and choose an item."
    End If
End Sub
In more complex applications with more than two activities you can use a process global variable in the main activity. Before starting an activity you can set this variable to some constant and then in Sub Activity_Resume check the value of this variable to know which activity was started and act accordingly.

The project is attached.


    6.7 KB · Views: 5,943


Licensed User
Longtime User
Because it gives me a mistake?


  • error.jpg
    139.4 KB · Views: 428