user2870
user2870

Reputation: 487

Do we need an activity class for each page?

I am a newbie in Android. I want to ask that do I need to create an activity class for each page in my app? If not what can I do to bring new pages on screen? Is layout inflater a good choice for this?

Thank you.

Upvotes: 2

Views: 926

Answers (3)

Guy
Guy

Reputation: 6522

Lets say, I have a list view which contains questions. When you click one of them you will see the answer o another page. This means lots of activity class. Then I am looking for another solution.

No, you don't need different activities for this. You can make your "Answer activity" dynamic. It can change data according to which ListView item was clicked. Here's a simple solution to your problem. Create a new int variable and change it accordingly to which ListView item was clicked. If item at index 0 was clicked, then this int should be 0. Pass it with extras to your answer activity and in your answer activity, implement an IF statement.

if (intVariable == 0)
{
    //show answer 0
}
else if (intVariable == 1)
{
    //show answer 1
}

Do you get the concept?

So, if you want to change the layout accordingly to which ListView item was clicked, this might help you.

Are you changing only image sources or actual layout positions? If you're changing positions, you might aswell create another activity because changing layout positions programatically would take too much time and it's too complicated.

Do you only want image sources to change but layout positions to stay the same? Then this is easier. This code might help you:

int position = //get int extras here
switch (position)
case 0:
    imageView1.setImageResources(r.id.yourPicture);
case 1:
    imageView1.setImageResources(r.id.yourPic2);

So as you can see, your image resource changed accordingly to ListView item clicked.

Upvotes: 1

yushulx
yushulx

Reputation: 12160

Not necessary, it depends on your design. A typical example is Android Gallery, which is implemented in OpenGL. All pages of Gallery are in one Activity. The advantage is the switching speed is fast, and UI seems much slick. Starting a new Activity may cost much and cause delay. So how to implement it this? You need to create a stack for storing all pages, and manually control(add or remove) them while switching different pages. Anyway, it depends on how you want to design your app(some pages could be displayed in new Activity, some pages not). You have to consider cost and performance.

Upvotes: 0

user2426316
user2426316

Reputation: 7331

Yes, to use activities is the most common way to display content. You can bring new content, that is new activities on the screen by starting new activities via Intent. Therefore, make sure to check out the activity lifecycle

http://developer.android.com/training/basics/activity-lifecycle/index.html.

On the other hand, it sometimes is useful to use fragments to display content on the screen. Fragments are basically "multiple screens" that are hosted by one parent activity. By using fragments you can achieve views like the one in the Skype android app.

Here is more on fragments: http://developer.android.com/training/basics/fragments/index.html

All in all: Use activities to display static content and fragments to make your views more dynamic.

Upvotes: 0

Related Questions