File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Changing screens - switching between layouts?

 
Adam Kronicki
Ranch Hand
Posts: 68
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 241
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Adam Kronicki
Ranch Hand
Posts: 68
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1369
1
Android Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Kacee Saxena
Ranch Hand
Posts: 54
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 241
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1369
1
Android Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1369
1
Android Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic