Download the free trial version
Basic4android Video
Features
Tutorials and manuals
Showcase
Screenshots

Go Back   Android Development Forum - Basic4android > Basic4android > Basic4android Getting started & Tutorials
Documentation Wiki Register Members List B4P Search Today's Posts Mark Forums Read

Basic4android Getting started & Tutorials Android development starts here. Please do not post questions in this sub-forum.

Two activities example

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-16-2010, 08:41 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default Two activities example

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:
Code:
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:



The second activity is pretty simple:
Code:
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.
Code:
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.
Attached Files
File Type: zip TwoActivities.zip (6.7 KB, 860 views)
Reply With Quote
  #2 (permalink)  
Old 11-16-2010, 09:01 AM
Cor Cor is offline
Basic4ppc Veteran
 
Join Date: May 2007
Posts: 481
Default

thanks

very clear

grCor
Reply With Quote
  #3 (permalink)  
Old 11-18-2010, 11:47 AM
derez's Avatar
Basic4ppc Expert
 
Join Date: May 2007
Posts: 978
Awards Showcase
Beta Tester Competition Winner 
Total Awards: 2
Default

Is it possible to use subs from the main in the second module ? I tried using main. before the name of the sub but it still shows an error. "Public" is not recognized...
__________________
David Erez
Ramat Hasharon, Israel
Reply With Quote
  #4 (permalink)  
Old 11-19-2010, 05:01 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

You can't call subs from other activities. Activities get destroyed and recreated as needed by the OS. The only data that is valid is the process globals (which are tied to the activities).

There will be other types of modules in the future which you will be able to use for sharing code.
Reply With Quote
  #5 (permalink)  
Old 11-19-2010, 05:43 AM
derez's Avatar
Basic4ppc Expert
 
Join Date: May 2007
Posts: 978
Awards Showcase
Beta Tester Competition Winner 
Total Awards: 2
Default

Erel
I'm afraid this is not exactly as you say.
I added a module with another activity, switched to this activity while running and when I returned to the main activity it continue to work as if nothing happened, although I haven't done anything to save and resume, or kept any variables at process globals (except GPS).
This way is perfect for this application, if only I could use the subs from the main module.
__________________
David Erez
Ramat Hasharon, Israel
Reply With Quote
  #6 (permalink)  
Old 11-19-2010, 05:48 AM
schimanski's Avatar
Basic4ppc Veteran
 
Join Date: Oct 2007
Location: Germany
Posts: 289
Default

Hello Erel!

I have one question about this thread. I want to rewrite my app from wm to android and now I dont' know, if I write it in one ore different activities. My app has several Forms, one main Form and the other forms are different forms for settings. When the main activity is a GPS-activity and I open a settings-activitiy, does the GPS-activity then stops working? If the GPS will stop, I think, that it is better to write everything in one activity and only load the new layouts to make the users settings.....
__________________
schimanski
--------------------------------------
Device: Motorola Defy, Samsung Galaxy Tab P1000
Dekstop: Asus Eee PC
Reply With Quote
  #7 (permalink)  
Old 11-19-2010, 06:35 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

Quote:
I'm afraid this is not exactly as you say.
I added a module with another activity, switched to this activity while running and when I returned to the main activity it continue to work as if nothing happened, although I haven't done anything to save and resume, or kept any variables at process globals (except GPS).
This way is perfect for this application, if only I could use the subs from the main module.
The OS may or may not destroy your activity while it is in the background, depending on the resources available.

For the discussion, why do you need to call the other activity sub?

Quote:
I have one question about this thread. I want to rewrite my app from wm to android and now I dont' know, if I write it in one ore different activities. My app has several Forms, one main Form and the other forms are different forms for settings. When the main activity is a GPS-activity and I open a settings-activitiy, does the GPS-activity then stops working? If the GPS will stop, I think, that it is better to write everything in one activity and only load the new layouts to make the users settings.....
The GPS is a process variable. It will not stop unless you stop it in Activity_Pause.
Usually you will want to stop it in Activity_Pause and start it in Activity_Resume (which will be called when the activity returns to the foreground).

You can also use one Panel for each of your layouts (panels can also load layout files).
Reply With Quote
  #8 (permalink)  
Old 11-19-2010, 06:52 AM
derez's Avatar
Basic4ppc Expert
 
Join Date: May 2007
Posts: 978
Awards Showcase
Beta Tester Competition Winner 
Total Awards: 2
Default

Quote:
For the discussion, why do you need to call the other activity sub?
The main activity is used for displaying the map, the other is for settings, handling of waypoints etc. The other option is to use a panel, to cover the whole display area and handle the settings from there. I chose second option after I couldn't use the subs from the main module, but now I have a problem with the panel not moving out of view when it is not needed...
__________________
David Erez
Ramat Hasharon, Israel
Reply With Quote
  #9 (permalink)  
Old 12-18-2010, 05:44 PM
Basic4ppc Veteran
 
Join Date: Feb 2008
Posts: 274
Default

BTW, how to delete non-actual anymore layout from the designer ? Excepting manual editing .b4a and deleting .bal.
__________________
LG P500, Android v.2.3

Last edited by peacemaker : 12-18-2010 at 05:49 PM.
Reply With Quote
  #10 (permalink)  
Old 12-19-2010, 06:58 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

Layouts are regular files. Go to the Files tab and remove the layout.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Android Process and activities life cycle Erel Basic4android Getting started & Tutorials 23 04-13-2012 09:25 AM


All times are GMT. The time now is 10:18 AM.


Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0