This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Android and the fly likes  Changing screens - switching between layouts? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Mobile » Android
Bookmark " Changing screens - switching between layouts? " Watch " Changing screens - switching between layouts? " New topic
Author

Changing screens - switching between layouts?

Adam Kronicki
Ranch Hand

Joined: Sep 01, 2009
Posts: 68
Hello,

I am trying to create an app for android 1.5 which would be organized
around 4-5 'screen's that will be changing in a given order.

So I start ma app with the first screen, which it's background and a
button. When I click on the button a new screen appears with a
different background and different options which lead to next screens.
Sth. that is usually seen in games. The problem is that I need to pass
a lot of data between those 'screens' like and array with recorded
sound. My idea was to create a separate xml layout file for each
screen and change the context in the activity class so ie:



A simple test app for 3 screens works this way but my I don't really
like this approach and it seems to be inappropriate.

Is this the correct way of organizing such an app in android or should
I take another approach - if I should, please give a more detailed
advice on how to do this.
Martin Vanyavchich
Ranch Hand

Joined: Sep 16, 2008
Posts: 241
First, I'm not very versed in Android developement, I have one app under my belt so don't take this post a something guaranteed to work.

I have created new Activities for each screen, data can be passed between these activities in Intents bundles/extras. But there's a limit to how much data can you transfer in such manner and it differs from one device to another. The other way is to use Androids SQLite or store your data on disk/SD card and using Serialization.

Whatever option will you choose, debug often on your actual device and look at how much memory does your application consume. Once you go overboard, Android just pulls the plug on you You canuse Eclipse MAT plugin fom memory checks and heap dumps.

Regards,

Martin


SCJP 6, OCMJD 6, OCPJWSD 6
I no good English.
Adam Kronicki
Ranch Hand

Joined: Sep 01, 2009
Posts: 68
Thank you for the contribution Martin . I learned about start new activities using the Intent objects. However I don't thing it is suitable for my idea. Intent are useful in apps that have one 'menu' screen which has many buttons leading to other screens. In my app I don't want any menu screen - each screen has only one button leading to the next screen(maybe in the future it would have a second one working as a 'back' button). So you start with screen A with 1 button, you press it and go to screen B with 1 button, you press it and go to screen C with 1 button, you press it and return to screen A.This forms a kind of 'ring' of screens. So I don't see a way to do it with starting new activities. Moreover I need to pass Java objects holding large arrays of data 'between the screens' and saving it on an SD card is not really an option. So your advice about intents and new activities is good and popular but not proper for my idea. But thank you for the tip about memory - I will definitely look at it more closely.
Kacee Saxena
Ranch Hand

Joined: Mar 04, 2008
Posts: 54
Even i had a similar query about "Passing data between different activities/screens". Currently in my application I am using the Java approach of "Has A" relationship. i.e. by providing getter methods in the Activity class and then other activities can instantiate the class object and call on getter method to access the data field.

Atleast this way, we dont need to worry about "passing data" . The fields can be accessed as an d when needed via its class object.

Any other better ways of doing this ... any suggestion are most welcome

Thanks,
Kacee
Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1369
    
    1

Kacee Saxena wrote:Even i had a similar query about "Passing data between different activities/screens". Currently in my application I am using the Java approach of "Has A" relationship. i.e. by providing getter methods in the Activity class and then other activities can instantiate the class object and call on getter method to access the data field.

Atleast this way, we dont need to worry about "passing data" . The fields can be accessed as an d when needed via its class object.


Any other better ways of doing this ... any suggestion are most welcome

Thanks,
Kacee

You are not supposed to instantiate Activity class directly. Use Intent Extras for passing data. Besides, if you are storing your data in instance variables wouldn't each instance have a separate copy of that data?


[List of FAQs] | [Android FAQ] | [Samuh Varta]
Kacee Saxena
Ranch Hand

Joined: Mar 04, 2008
Posts: 54
I have declared instance variables as static. Here , in my application, I wanted to make username as a "global" variable, as it would be displayed on each screen.

I personally found "getExtras and putExtras" more preferable where one or few activity classes need to access data variables of a particular class.

Any expert comments are most welcome

Regards,
Kacee
Martin Vanyavchich
Ranch Hand

Joined: Sep 16, 2008
Posts: 241
Declaring variables as static seems the easiest way, I wonder how it affects memory and performance. I've heard of another way but never implemented it. Your Application object is accessible from whatever point in your app. So you extend Application class and add variables that you think should be visible in a 'global' way. Then you will have to do some changing to your manifest and some other stuff

If someone tries this please report back
Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1369
    
    1

I have declared instance variables as static.

When you do that they are no longer called instance variables.
Declaring variables as static seems the easiest way, I wonder how it affects memory and performance.

Static variables are held for long; only JVM decides when to mark them for garbage collection (usually when the class unloads or JVM terminates).
Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1369
    
    1

I've heard of another way but never implemented it. Your Application object is accessible from whatever point in your app. So you extend Application class and add variables that you think should be visible in a 'global' way. Then you will have to do some changing to your manifest and some other stuff

If someone tries this please report back


You can have a class extend Application class which will be instantiated when the process for your application/package is created. When you do that in your applications manifest you will have to specify the Name of the class file in android:name attribute.

You can have variables of any type, its getter and setter methods in this class and then access them from any Activity. The way to do this would be:



Hope that helps..
 
wood burning stoves
 
subject: Changing screens - switching between layouts?