14

When i started my Android project i had a misunderstanding that every screen that's shown in the application must be a new activity. Now i am finished with the project , i have checked it on my emulator as well as on a couple of android phones. So far i am not witnessing any problems, but recently i read somewhere that too many activities in the application are a pretty bad idea.

Currently my application has around 15-20 activities.Ideally i heard it should be around 5-6. Do i need to re-structure my code or just finishing every activity after it has done it's part is enough?

Desert Ice
  • 4,461
  • 5
  • 31
  • 58
  • Is each activity a completely different screen? Or do they only differ by the data that is displayed? You may be able to combine classes which extend Activity by refactoring. – Code-Apprentice Jun 29 '18 at 20:23

6 Answers6

39

While creating complex applications you definite need to create many activities. So it depends on your application how many activities you need. No of activities in a project do not affect the performance.

The effect is produced by the number of activities in the stack of your android. So it is better to keep 5-6 activities in the stack(Finish activities if they are not needed any more).

So create as many Activities as your application demands but keep smaller no of Activities open at a time.

Muhammad Nabeel Arif
  • 19,140
  • 8
  • 51
  • 70
8

If your project has many activities but some activity is not important,it means that you do not need any activity after another activity start.

In manifest file set : android:noHistory="true"

Example:

Activity1 -> Activity2 -> Activity3 -> Activity4..................-> Activity20

In manifest file:

  activity android:name=".Activity1" android:label="@string/app_name" android:noHistory="true"

if u call again Activity1 using Intent than set finish() before startActivity()

I think this can help you

Echilon
  • 10,064
  • 33
  • 131
  • 217
Gandhi Ishan
  • 149
  • 4
4

The Android system tries to maintain an application process for as long as possible, but eventually needs to remove old processes to reclaim memory for new or more important processes. This applies to Activitys that are running in the background... old Activitys are managed for you and are destroyed when the system needs to reclaim memory for new processes.

That being said, I think there are two things you should consider:

  1. User experience. Does your app really require 15-20 Activitys? Can you cut down the number of screens somehow? Less Activitys is usually better, since it requires less interaction when the user is navigating the application.

  2. Code design. While each Activity will have its own separate class, this does not restrict you from making smart design-decisions when implementing your application. For example, you might try grouping similar Activitys by having them extend an abstract class. As Android projects grow in size, they become more difficult to manage. Sharing code amongst similar classes in this manner will ensure that you can make simple changes to the core of your application without too much trouble.

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
  • I have 15 activities that the user is constantly opening. Do I need to do anything to close the previous activity? Thanks! (+1) – Ruchir Baronia Jan 04 '16 at 02:06
3

[EDIT] - As of Google IO 2018, Google recommends using a single activity with many fragments. Something to consider.

It ultimately depends on what you're doing. There are some times when you can not modify the view enough to make a difference. Ideally, 5-6 activities is great, but some cases thats not just not doable. I've done a mobile app with around 40 different classes, and about 18 activities. It just HAD to be done that way based on how the app was to interact with the user. If you can merge 2 or 3 activities into one, thats great. It'll help with file size and optimization as well, but if you can't- Don't fret on it too much.

Brad Cypert
  • 671
  • 1
  • 8
  • 29
1

I would say 15 different screens = 15 different activities. I think one of the reasons some are able to reduce the number of activities is because of the introduction of fragments. Although one will argue why use fragments if individual activities works. I guess it depends on the developers preference.

Mark Pazon
  • 6,167
  • 2
  • 34
  • 50
-1

It's better to use fragments than activities. We can use multiple fragments in single activity so we should always consider using fragments. I've seen complex applications with 5-6 activities and 150+ fragments.