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:

two_activities.png



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:
B4X:
Sub Button1_Click
    StartActivity(Activity2)
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:

twoactivities_logcat.png


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

Sub Globals
    Dim ListView1 As ListView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("2")
    For i = 1 To 100
        ListView1.AddSingleLine("Item #" & i)
    Next
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.
B4X:
Sub Activity_Resume
    If Activity2.result.Length > 0 Then
        Label1.Text = "You have chosen: " & Activity2.result
    Else
        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.
 

Attachments

  • TwoActivities.zip
    6.7 KB · Views: 6,099

gkumar

Active Member
Licensed User
Longtime User
Process_global variable will not hold data when we switch to another activity

I have string declared in Process_Globals of main activity. When I switch to another activity, the variable is not holding the data. I want this variable to be hold the data for another activity. How to overcome this problem?
 

gkumar

Active Member
Licensed User
Longtime User
Process_Global variable not holds the value

Declaring the variable in the Process_Globals of Main activity as
Dim gStrToken As String

In the Parser_EndElement method assigning the value as,
gStrToken = Text.ToString.Trim (Verified that value has been copied)

And in the Activity_Create of another activity accessing the value as,
Main.gStrToken
where the value is empty.

Whts wrong here?
But in another way, I added a New code module to store this value, it works fine for me. Let me know whether anything is missing here.
 

Kevin

Well-Known Member
Licensed User
Longtime User
I've been having similar issues lately as well.

I have a list object (not listview) in my main activity. The user can start a second activity and that activity reads in the data from the list in the main activity (the list is a process global in the main activity). This data is read each time the second activity starts and is used to populate a listview.

This normally all works well. However, in the second activity the user can press a button that opens a web page in the device's default browser. If the device gets low on memory while the second (and first) activity is paused while the user is in the browser, then Android seems to be killing the first activity (and possibly the second).

My problem then arises when the user backs out of the browser and the OS returns to my second activity. At that point I get an error when the second activity tries to populate the listview with the first activity's list data saying that the list in the first activity is not initialized.

Since the source list is in the first activity and I can do nothing about it, the only solution I have come up with is to kill the second activity (ExitApplication) if a check of whether or not the list in the first activity is initialized comes back as false. This then kills the second activity before it ever really shows again and sends the user back to the first activity (and I assume it is then recreated).


I think part of the issue is with my phone. I have an EVO 4G and over the past few updates to it they have cracked down on killing background processes. As a result, background processes tend to get killed way too often. The biggest problem with the EVO 4G now is that they got so agressive with it that the "Sense" parts of the phone (basically HTC's custom interface and widgets) get killed way too often.

It's so bad that if I use the EVO's built in RSS widget and open a story in the default browser then I can almost always kill the Sense process on demand simply by doing an everyday thing such as reading a few news articles. Sometimes the phone even kills the browser while I am reading a story and dumps me back to the RSS widget app. To add insult to injury, many times when I get dumped back to the RSS widget (or just return to it after reading a story) the widget is empty and says there are no stories even though there were 100 stories before I tried to read the article. This is of course because the widget itself is getting killed and recreated.

On the plus side, it makes it easy for me to see what happens when my app gets killed by the OS. All I need to do is press Home, read a couple articles and wait for the phone's interface to completely restart then return to my app. :D
 

Kevin

Well-Known Member
Licensed User
Longtime User
Sounds like that would probably work! I should have thought of that myself.

I'm working long hours the next few days but when I get a chance I wil try this.

Thanks!

What are the chances of Android killing a code module while my activity or activities are paused?
 

Kevin

Well-Known Member
Licensed User
Longtime User
I had some time to test this today and I guess my phone must be killing the whole process.

I had moved the list from main to the code module. In the second activity I create a new list based on information found in the other list (now in code module). When I forced my phone to kill off its processes and then returned to my app, neither list was still initialized. Both the list in second activity and the original list now Dim'd in the code module came back as not initialized. I tried initializing the original list in the code module but then I once got an error in my main activity where it said the list was not initialized.

Oh well. I guess I'll just have to leave it how I had it, where the second activity checks to see if the lists are no longer initialized and if not, just kill the second activity through code. It's not perfect but it sure beats an empty page or an error that a list is not initialized. :eek:
 

gkumar

Active Member
Licensed User
Longtime User
Process_Global variables not holding the value

Still application is not holding the Process_Globals variable values. Any other settings missing?
 

Kevin

Well-Known Member
Licensed User
Longtime User
Yes, I think that is just about my only option. More work again, when I was hoping to be ready to push an update, but it will be better for it. I'd probably end up doing it at some point anyway.
 

sioconcept

Active Member
Licensed User
Longtime User
Hi all,

I've the same problem. I've 2 activity; when i start the second activity, the first activity is destroyed, so when i back again on the first i lost all data. How i can switch without lost data ? The data is some texts, panels, labels etc.. so i can't save it directly.

Have you find a solution ?
 

ADPTraining

Member
Licensed User
Longtime User
Too many panels is not a good idea...

Just for the record... I've been working on an APP and have added up to 8 panels(.bal) with many views. At one point the software decided not to make any more views. So, I removed one panel with it's dedicated views and it all started working again. I did not get any errors, just that the lastly created views did not show or only the Button text would show.

I ran into this issue years ago with Visual Basic in putting too much stuff into one form. I knew it would happen here sooner or later. So, the best and only programming practice (if your APP has lots of views/controls) is to divide into separate Activities and call them as needed. For large APPs Activity calling is the only way.
 
Top