Is one Activity enough for Android app


In Android , everything visible is a View .But knowing what is behind every view is challenging if you are starting android development . So the base (and this is not applied only for android) is some XML to create views and the JAVA/KOTLIN . In Android , the Views define an Activity which is described by lots of other views : TextView , EdiText , Button etc .
According to the official documentation in Android Developers web page , this is the Activity's definition :
An activity is a single, focused thing that the user can do. Almost all activities interact with the user, so the Activity class takes care of creating a window for you in which you can place your UI with setContentView(View). While activities are often presented to the user as full-screen windows, they can also be used in other ways: as floating windows (via a theme with R.attr.windowIsFloating set) or embedded inside of another activity (using ActivityGroup)
So , when you create an empty Android App , the IDE by default generates a MainActivity.java , connected with an XML file on the setContentView(View) method . For each user action , you create one activity . Example : When you try to login in Gmail , there is a view which is asking you to input your credentials named LoginActivity, and after a successful login , you might get inside a new activity that could be named InboxActivity to check your latest mail. On the other hand ,we can't ignore fragments. Using too many activities in one app might have it's drawbacks. So what are fragments ? Check the documentation definition :


You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running (sort of like a "sub activity" that you can reuse in different activities).

In short terms, one activity can have one or more fragments . Now I am adding some additional info from my own experience . The question is this : Why note make an app with a single Activity holding as much as required Fragments . Well I would say , this is completely doable . The reason is simple , one Activity is more than enough to make an Android App . Why would I used it this way ?
- Fragments are lighter than Activity when it comes to memory usage .
- If you know how to use fragments properly , you will produce a lighter app than expected .


NOTE :
Using Fragments in android sometimes can be tricky . You need to have good knowledge in Context ,  Fragment Backstack and Transitions (optional) .

Comments

Popular Posts