Too many Activities in Android?

后端 未结 6 1954
春和景丽
春和景丽 2021-02-13 14:24

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

相关标签:
6条回答
  • 2021-02-13 14:51

    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.

    0 讨论(0)
  • 2021-02-13 14:53

    [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.

    0 讨论(0)
  • 2021-02-13 14:56

    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.

    0 讨论(0)
  • 2021-02-13 14:57

    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

    0 讨论(0)
  • 2021-02-13 15:03

    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.

    0 讨论(0)
  • 2021-02-13 15:10

    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.

    0 讨论(0)
提交回复
热议问题