22

When do I need to create a new activity and when do I need to change the view?

My app needs to do:

Screen #1

two big buttons(sort of menu)

Screen #2

list of items - depend on the selection on prev screen

Screen #3

another list - depend on the selection on prev screen

Screen #4

show item

All screens need to have the same menu menu (the last has another button)

Do I need to create an activity for each screen or just change the view in the same activity?

Maybe I need to create a parent class myBase that will extend activity and all my activities will extend him?

RBT
  • 24,161
  • 21
  • 159
  • 240
eyalb
  • 2,994
  • 9
  • 43
  • 64
  • 1
    Related posts - [Android - I need some clarifications of fragments vs activities and views](https://stackoverflow.com/q/10478233/465053) & [Difference between View and ViewGroup in Android](https://stackoverflow.com/q/27352476/465053) – RBT Aug 14 '18 at 09:40

6 Answers6

18

A View in Android is a widget that displays something. Buttons, listviews, imageviews, etc. are all subclasses of View. When you say "change view" I assume you mean change the layout by using setContentView(). This usually only needs to be done once per activity. An Activity is basically what you are referring to as a screen. To answer your question, it sounds like you need four separate activities (one for each screen).

Amplify91
  • 2,690
  • 5
  • 24
  • 32
17

You should create separate activities for you screens. Android handles the back button of the device by popping the current activity out from the stack and displaying the last one. So if for example the user wants to return to screen 2 for another selection, the back button does this.

Bogdan M.
  • 1,726
  • 3
  • 15
  • 24
10

The "right" way to do it is to use Activity for each screen and use the <include> tag for the menu you would like to be in all screens.

This way you will have the "back" button act as it should be and it would be easier for you to handle when switching screens.

To use the , you should put the things you want to reuse into extra files. Then you can use it as follows:

<!-- my_header.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/text01"/>

In another file include it with:

<include layout="@layout/my_header" />
<!-- your other stuff -->
Asaf Pinhassi
  • 15,177
  • 12
  • 106
  • 130
3

activity is like canvas where you put your drawing as view.Yes you can set all above four view in single activity but it will depend how you handle it and does your app needs it to be done like this.

Vineet Shukla
  • 23,865
  • 10
  • 55
  • 63
  • I think you should use different activities as you described screen#2 depends upon screen#1 and screen#3 depends upon screen#2. – Vineet Shukla Aug 05 '11 at 06:23
  • -1 Activity is not like canvas. You display things with both of them, but they are two totally different things! – Amplify91 Aug 05 '11 at 06:32
  • @Amplify91 just tried to explain him and I am not saying activity is canvas.Don't go for words. Just trying to explain what activity does in android for view point of view and what I understood for view it provides platform as well as you define your task within activity as well. – Vineet Shukla Aug 05 '11 at 06:37
  • Activity is totally like a "canvas" or a "window" in android. not a Canvas class, but a thing that gets something drawn on it and gets plotted to the viewport of the device. (not like a Canvas class - also yes you can draw on the screen without an activity) – Lassi Kinnunen Nov 22 '18 at 04:15
2

View is Display System of Android where you define layout to put subclasses of View in it eg. Buttons, Images etc. But Activity is Screen System of Android where you put display as well as user-interaction,(or whatever which can be contained in full-screen Window.)

Now for your question,you are creating full window screen#2 ,screen#3... ,so it is activity. In these screen you can define layout/or Views.

I hope it helps.

vivek
  • 356
  • 3
  • 7
1

You should create 4 xml files... and play Around changing content using setContentView(R.Layout.yourxml);..

you can do this using single Activity.. it depends on how big the class becomes.. if its too heavy with lot of different stuff, for the sake of cohision and to avoid coupling use multiple Activities

ngesh
  • 13,398
  • 4
  • 44
  • 60
  • 1
    It is possible to do it this way, but Android recommends a different activity for each "screen". – Amplify91 Aug 05 '11 at 06:33
  • 1
    if there is nothing serious going on in Activity and just changing views i don't think multiple Activities are needed.. but in other case i agree your right.. – ngesh Aug 05 '11 at 06:36
  • As an example, if you have an application which communicates often with the server, it might be useful to have different activities for each screen, otherwise your application might crash because of lack of responsiveness –  May 17 '14 at 18:48