Skullbox
Skullbox

Reputation: 441

Simple android app, what structure should I use?

I have read about different layouts but still can't understand how to structure my simple app.

All I want is a screen with an image and a set of buttons. When you press a button another screen slides in from the side. This second screen has an image and its own set of buttons.

Sure for this I could use two separate Activities. BUT I want both screens to have access to some variables I've declared. After reading about activities, it seems using a parent and child activity is not the right solution, since the parent activity can be dropped from memory so my variables would be lost.

So if I have one Activity and want two screens content to interact what is the best way, what structure should I use for this?

Upvotes: 0

Views: 154

Answers (2)

tritop
tritop

Reputation: 1745

Thats what Fragments are for. See the api guide or just google for a tutorial. You would be able to communicate between Fragments with callbacks, but you don't necessarily need to, since you could just call static classes on button click. That way you wouldn't need to communicate too much. The communication is kind of hard to describe since we don't know what the buttons will do though. But still: Consider using Fragments. They are designed for doing exactly that and there are a ton of tutorials showing how to do this exact thing. The transitions will be way more smoth than between Activities.

With Fragments you would have one Activity that hosts several Fragments that host Imageviews and Buttons. You could implement swipe or onclick or whatever you want to use to switch, then do a Callback on that, returning data with it if you want to. Through that Callback the coresponding function in your Activity is called, where you could create the next Fragment, add data to it and replace the old Fragment with it.
It would be a bit too far to explain that in deepth because how Fragments work would be a different question. But thats essentially what you would do with them.

If you decide to use Activities, you should go with the putExtra to intent way.

Upvotes: 1

LittleXenomorph
LittleXenomorph

Reputation: 325

You can pass data between activities using an Intent. Add your desired data to the Intent and start the new activity.

Intent i = new Intent(this, SecondActivity.class); i.putExtra("one", "someValue"); i.putExtra("two", "anotherValue"); startActivity(i);

Upvotes: 0

Related Questions