The relationship between activity and its layout in Android

In Eclipse, anytime I create a new activity in my Android application, in addition to the Java Activity class, an accompanying layout XML file is also created. Obviously, I could generate 10 layouts and switch the activity layout between these 10 at any time.

So my question is why this layout is being created; Is this generated for convenience or is it a good practice to always have a specific layout related to an Activity?

Also, an Activity should always have a layout or that I can have an Activity that never uses a layout.

+4
source share
1 answer

From android help:

Activity is a single focused thing that a user can do. Almost all actions interact with the user, so the Activity class takes care of creating a window for you in which you can place your user interface using setContentView (View).

This process of creating a window is an expensive operation, and you only need to call setContentView-once in the Activity .

  • If you need to add xml resources, you can use the layout of the inflatable system . (tip: always transfer container link to inflate)
  • If you think you need to use more than one layout file (a whole new ui), you should consider creating a new Activity .

About your questions:

So my question is why this layout is being created; Is this generated for convenience or is it a good practice to always have a specific layout related to an Activity?

Convenience, you use the wizard to create an Activity. In most cases, you implement an XML resource for your activity, so the wizard creates it for you.

Also, should an Activity always have a layout, or do I have an activity that never uses a layout?

It is not required to have a layout for the Activity, but a window will be created. An example of this is a splash screen in which you display an image but do not set the presentation of the content.

+4
source

Source: https://habr.com/ru/post/1498759/


All Articles