Android programming, layouts and activities

jwatte's picture

I've been doing some Android programming, trying to get a feel for what an Android app would look like when structured as a main screen, some set-up screens, and a main gameplay screen (which then wants to go back to the main screen). I've fought a bit with the built-in Android layout views. They seem to be missing some capabilities that would be really useful. Also, if you write your own subclasses, you can't actually use them in the Eclipse layout/view editor, unlike tools like Visual Studio where you just drag your new classes into the toolbar and lay them out like anything else.

I found that you pretty much need to use only LinearLayout and RelativeLayout, and adjust spacing using layout margin. You also have to nest layouts a fair bit. I think TableLayout is sometimes useful, but the other layout managers are just crap (and, in fact, the relative is also fairly crappy).

There are two fairly big problems:

1) "fill parent" seems to always want to fill the entire screen, unless the parent is sized to a fixed size. Fixed size is of course bad. Thus, you can't do something like put two views into a layout, make one of the "wrap content" and the other "fill parent" and make the "fill parent" just be the same size as its sibling. In Relative layout, you can do this by setting both "layout align left" and "layout align right," but for others, you're screwed.

2) There is no "space evenly" option. If I want four items of different size to space evenly stretched across the screen, there's no good way of doing that. Table layout is the closest you can come, but it doesn't quite do the same thing. Linear layout with a "space" option, and a fixed "fill parent," would get a lot closer.

I probably should let the Google people know, rather than you, huh? :-)

Second, I have a problem with the Back button, and making a PendingIntent resume a currently running Activity. When the user presses Back, it kills the activity, but if the user presses Home, then my task and activities are still instantiated, but hidden from view. However, there's no good way to get back to whatever the user was doing, with the "back" stack intact.

Here's my game flow:

1) Main screen
1a) New game configuration screen -> pops back to Main screen
2) Game screen -- started by the main screen, intended to be further down on the stack

Now, if the user presses Home, I want to be able to get back to the Game screen instance for as long as it's still in memory. I want to get there with the "back" history intact -- "back" from the game screen should go back to the main screen.

I tried adding a status notification (ongoing) with the game class as intent, but that tried to create a new instance of the game, rather than resume the existing one. I had to make the game activity singleInstance for that to work -- but then the game runs in its own Task (process), separate from the setup/configuration. At least I have a Service for the shared state (yay!) but it screws with the back button -- after resuming, Back from the game itself goes back to the home screen, not to the game setup screen.

We'll see if I ever figure this out, or if I'll have to structure the app as two collaborating tasks -- when the game is done, "continue" re-starts the main screen activity.