I have 8 different activities that open each other on a button click randomly. After clicking the button that opens a new activity around 15 times, it crashes. Do I need to close the activities or is there another solution?
As Harsh Pandey said, you may want to look into using fragments instead of activities. Another possible way is to call finish(); right after you start the next activity. This will close the previous activity. However, if you ever intend to go back to this activity using a back button or something, it will not exist.
When you create new activities and close them, GC will automatically clean up the memory used by the old activities when required. You are getting an out of memory error because,
Your activities are holding up a reference which is still alive. This will prevent GC from cleaning up the old activities.
for example,
Look for animations which are not cleared properly.
The best way to deal with this is find the memory leak.
https://developer.android.com/studio/profile/investigate-ram.html
Suggestion:
As 3kings mentined, try to do in one activity. ie Use Fragments !!
You can request more heap size by adding this in your manifest:
android:largeHeap="true"
However, based on your comment, my guess is that this case is ideally suited for fragments, instead of activities. You can accomplish all of that in a single activity, with multiple reusable fragments.
Related
My application has a logical Activity hierarchy, as follows:
I'm trying to navigate across Activities while checking if they're running. For instance:
Main Menu > Activity A1 > Activity A2.
Now if I go back to Main Menu, is there a way to check if Activity A2 is already running, and if so, jump directly to that Activity?
I think setting android:launchMode="singleTask" to Activity A1 might do the trick, and read about the android:allowTaskReparenting attribute. But i'm unsure exactly what to use to achieve what I want.
It would be easier to use one host Activity (as a navigation controller ) and many Fragments. Current Fragment will callback to the Activity, so it will make decision how to navigate on your graph.
If you really want to stick to the graph with many Activities, default behaviour - all activities in the same process will be alive. Android OS can only clean/kill process, not Activities (documentation a bit vague about it). You can use Intent.FLAG_ACTIVITY_NO_HISTORY for middle activities, so only root one will be on the stack.
There is a way to investigate back stack with ActivityManager, but it's deprecated.
I vaguely understand what fragments are used for. However, I don't understand why ADT seems to suggest that we should ALWAYS use them. I am aware that I can simply delete the fragment layout, as well as the code in Activity for the fragment. But is it ACTUALLY recommended to use fragments all the time? What are the benefits of not deleting it?
Is there even a point of an Activity having a single fragment? What is the difference with it having no fragments at all?
Additionally, how do you know whether to create multiple fragments in one Activity, multiple Activities with a single or zero fragments or a combination of Activities and fragments?
For example, if you have a Terms and Conditions button in your Activity, and you want it to open a screen containing a document listing the terms and conditions, should I be starting a new Activity for that? Or should I just move a fragment containing this content to the front?
Using Fragments is how modern apps are developed today.
Some of the benefits are a better performance when switching content (lets say 2 fragments)
because you are not leaving the current Activity.
Also, apps with swipe tabs (for example) are built with an Activity as a container and Fragments as the content itself.
Other benefit is that you can do much more with one activity, distributing work between the fragments. Each Fragment have its own Layout and therefore its own components, so maintaining the code and keeping organized is easier.
Eclipse does this way as suggesting a start point. You can totally ignore this and start from scratch. And that's all.
I suggest you to keep the Fragment to start getting used to it. Even if there is just one.
And, if later you need to work more with that Activity, it will be easier to add new features by creating a new Fragment, without modifying your previous code.
Good luck.
I have an application that is very image-intense, and I'm finding that I'm running into alot of OutOfMemory issues when loading up multiple activities.
The activities are all gridviews or listviews of bitmaps, and clicking on an image takes you to another activity that contains another set of images (sort of like an album of albums of albums of albums). The first 3 activities run fine, but when navigating further down, I start running into some serious OutOfMemory Errors.
After doing a stack dump with DDMS, it appears that the GridViews and Listviews of the previous activities are hogging all the memory. This is kind of expected, since they are showing lots of bitmaps themselves, but I'm not sure how the VM goes about freeing activities in the stack, and if they do it even when you havn't called "finish()" on them.
Do Activities recylce their views when they go into the background and then restore them when you navigate back to the activity, or is clearing the imageviews in my gridviews and listviews this something I need to handle manually on the activities onPause() and onResume() when navigating away from the activity in the lifecycle functions?
Please see my answer on OOME
Always call bitmap.recycle() after using bitmaps, since GC cannot clear memory held by Bitmaps.
The above link has a generic solution, please go through it.
consider recycling bitmaps onPause
Hi i am working with android , and i have a problem with the activity stack. As i know, when someone uses the back button, reload the back activity. But in the case i have many layouts shown from one activity, how can i go back to them.
Here is the deal, i am using a listview filled with categories, and when i press an item, i reuse the activity and the layout, to show its subcategories. So what i need is to came back no to the back activity, not to the back layout, but to the back "state".
Well, the idea is simple, first i show all the categories with no parent, then when i pressed an item, i show its subcategories.
The easiest way is creating two Activities - for categories and for subcategories. If you try to implement all the logic in a single Activity you won't earn nothing and just end up totally confused. Using Activities simplifies things a lot just because it handles problems such as yours. Hope this helps.
Check out Fragments, they are the stepping stone between a view and an activity. An activity can have multiple fragments and will manage their back stack (if you tell it to).
http://developer.android.com/guide/components/fragments.html
You'll have to use the support library to used them on pre honeycomb devices.
I have a GameActivity. I also have a transparent ChatActivity running on top if the user presses the Chat options from the Menu (onOptionsItemSelected). The problem is, when a player starts the ChatActivity before I start the game, an odd behavior occurs and the game won't start.
Is there any way I can keep GameActivity active while ChatActivity is visible?
I fired up the ChatActivity using the normal way:
startActivity(new Intent(GameActivity.this, ChatActivity.class));
Thanks for your help.
You can't have two activities in one activity. However; one design you could possibly achieve is introduce a design that allows the user to swipe the screen to bring up the chat view and swipe in the opposite direction to hide the view.
Have the main Activity that is running maintain that view via an async process so that it can be updated as necessary and does NOT interrupt the user.
You could take a look at FLAG_NOT_TOUCH_MODAL (and maybe FLAG_NOT_TOUCHABLE), using 2 activities on top of each other, making the top one transparant and give it that flag/those flags (not sure if you can actually touch windows you create within that activity, didn't try that yet)
you could also take a look at this. It's not exactly the same thing, but maybe you could rework it a bit to fit your needs
I know this is very very late,but this answer could be of use of anyone new.
For achieving the kind of design mentioned here, you could implement fragments instead of having two activities.