Reputation: 1875
So, I have nearly 20 Activities because I have nearly 20 Layouts in which are important information (concerning the app content). Well, I have to say in these Activities are mostly TextViews.
Is there something like a "Stack" where Activities can be put? If there is a stack, does it affect the perfomance when running the app? How can I see this stack?
Is there a rule of how much Activities an App should have or does it depend on what the purpose of the App is?
It would be nice if you can explain me how the Activities are held or how they are handled when I run my app. Because I do not want that these 20 Activities to affect my performance.
Upvotes: 4
Views: 1013
Reputation: 4296
Those 20 activities will influence only your apk size. But if they have just a TextView, this will not be considerable.
If the user navigate through all 20 activities and they are kept in a stack, something like activity A call activity B, that calls activity C ..., your activity stack will be too big and probably android will destroy the oldest one to free resources. With this, it is very important that you implement all the activity lifecycle and keep the data in the bundle so when the user returns back to your activity, you can recreate it from scratch in the exact same state of it was previously.
--EDIT--
As pointed out by @CommonsWare in the comments, Android will not kill your oldest activity, please check this post in his blog for suggestions if your case matches from what I described in my answer: https://commonsware.com/blog/2011/10/03/activities-not-destroyed-to-free-heap-space.html
Upvotes: 2
Reputation: 29844
First question:
Is there something like a "Stack" where Activities can be put? If there is a stack, does it affect the perfomance when running the app? How can I see this stack?
Yes, you can arrange Activities in a task which organizes them in a back stack. Refer to the documentation on how to work with them.
A task is a collection of activities that users interact with when performing a certain job. The activities are arranged in a stack—the back stack)—in the order in which each activity is opened. For example, an email app might have one activity to show a list of new messages. When the user selects a message, a new activity opens to view that message. This new activity is added to the back stack. If the user presses the Back button, that new activity is finished and popped off the stack. The following video provides a good overview of how the back stack works.
Source: https://developer.android.com/guide/components/activities/tasks-and-back-stack.html
Second question:
Is there a rule of how much Activities an App should have or does it depend on what the purpose of the App is?
Yes, the number of Activites your app needs depends on the use case and even your style of structuring your project. You could also use Fragments and replace them in one Activity when needed for instance. There are prons and cons for both ways.
Upvotes: 3
Reputation: 113
It depends on how you use them. If you learn the lifecycle of activities on
You ll see that if you keep a lot of activities on the stack then you ll loose performance, but if you destroy them, you ll not. You will just affect apk size.
Upvotes: 1
Reputation: 331
There is no performance difference between having 20 activities layered out in one, or having 20 classes running in one activity. Just the opposite, the former would have a better performance because you can dynamically add/remove activities, while the latter will have a fixed load of all of them.
Upvotes: 2