Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it a bad practice to have only one Activity in your Android app that uses fragments?

I'm wondering if this is a 'no no' in the Android community.

My app just has a MainActivity and uses a ViewPager and TabLayout to navigate across the fragments in the app.

The only problem I see is if the user presses the back button, it will exit the app and the app will not stay active like it would by pressing the home button.

Your thoughts?

like image 968
God Usopp Avatar asked Nov 28 '15 13:11

God Usopp


People also ask

How many activities should an app have?

Typically, one activity in an app is specified as the main activity, which is the first screen to appear when the user launches the app. Each activity can then start another activity in order to perform different actions.

Is it better to use fragments or activities?

We can't create multi-screen UI without using fragment in an activity, After using multiple fragments in a single activity, we can create a multi-screen UI. Fragment cannot be used without an Activity. While Using fragments in the project, the project structure will be good and we can handle it easily.

What is the advantages of fragments over activity?

Activities are an ideal place to put global elements around your app's user interface, such as a navigation drawer. Conversely, fragments are better suited to define and manage the UI of a single screen or portion of a screen. Consider an app that responds to various screen sizes.

Are fragments faster than activity?

Each tab will have a fragment to hold the products. The tabs could either held in activity or a fragment. I do find fragments a slightly faster than activities but really its not something you would really notice in most cases. Regardless if they was intended for speed or not they still seem/feel little quicker.


1 Answers

Nice question bro, Few months back I was thinking in sameway.

You are 100% right, you can do it without any trouble, it only depend on your project and what do you want to achieve.

You could control your fragments from a single activity, beacause all fragments are independent of each other.

The limitation is :

One fragment should never talk directly to another fragment, you have to go through the parent activity

Only some imp points are:

  1. You need to learn all details about fragment.

  2. You have to manage the order of the fragments.

  3. It add lbit complexity in code

One Activity and all other Fragments

like image 171
Sanket Prabhu Avatar answered Oct 10 '22 16:10

Sanket Prabhu