2

I develop android app and have memory management problems. I have ability to navigate through infinite way like:

User activity that contains followers of user (U) -> Another user (U) -> post (P) -> U -> U -> P -> ... etc

So, basically I'd made it just through starting new activity, but i had noticed when I open about 6-7 activities it crashes with memory error. Each activity starting to eat more and more memory. First of all - finish() previous activity before start new one is not good, because i need to let user get all way back. Second, I'd tried to fix it with

FLAG_ACTIVITY_REORDER_TO_FRONT 

to use already initiated activity, instead starting new one. But it not redrawing it on first use. For example when i restore post activity when it comes first time it comes with previous post, but when i go back and opening it again all working good. But still no luck.

What the best way to organize this kind of infinite stack? It's able to make stack store for example 5-6 previous steps and close very deep activities. For example- is it able to store full activity state to some storage and organize activity stack manually?

Regards

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • it's possible but its wrong way. Use Fragments instead of activity if your case look like infinite loop – once2go Jun 08 '16 at 14:38
  • And, by the way, add more code or your draft window switching flow. It can be more understandable – once2go Jun 08 '16 at 14:39
  • 1
    I work in a similar app where a infinite navigation is possible. The app is a very photo intensiva app and relies on `Picasso` for its photo tasks. 1st of all, unfortunately that's how Android works. It's sad, but that's how it does. But only 6 or 7 activities seems very little for me and it indicates that you're likely to have a memory leak somewhere. On my app I can get easily to 60 activities deep before running into issues. So I suggest you check https://github.com/square/leakcanary to try to find possible leaks on your app. – Budius Jun 08 '16 at 14:40
  • @once2go I understand that this is bad way in case of Android, but that is a task what we need to resolve. Any way when we will use fragments instead of activity don't really believe that this will solve problem. And i think that way more wrong than use many activities. You can check pinterest app: it's have infinite stack and you able to get all way back through stack. – Alexander Bochkarev Jun 10 '16 at 00:34

2 Answers2

0

Try setting android:launchMode="singleInstance" at the activity in the manifest. (courtesy of this answer https://stackoverflow.com/a/9598551/2808913)

If one of those activities is your 'home' Activity, the Activity you generally start in in the app and return to, you can set it to start as FLAG_ACTIVITY_CLEAR_TOP. This way every time you return to this Activity your backstack will be cleared. Pressing the back button will make you exit the app instead of going back to the previous activity.

Community
  • 1
  • 1
miva2
  • 2,111
  • 25
  • 34
  • Yep, that make sense, but when new instance are coming at the top of stack - there is no way to get back. I had found something interesting here http://stackoverflow.com/a/3473377/997867 i understand that this is not better way, but what do you think about it ? – Alexander Bochkarev Jun 10 '16 at 00:31
  • Hmm, I think if you want to do it manually you should use fragments. I know many people don't like fragments but that solution is even uglier. Even the writer doesn't recommend it. At least fragments are pretty standard in Android :) – miva2 Jun 10 '16 at 10:58
0

There are not enough place on comments to write all my things, thats why i'm try to describe solution hear. As i can see you don't have different functionality in your content switch flow. So, do you know about Recycling pattern? You can create something like adapter with only 2 fragments(for user & for post) entities, where data binding will be produced from key value map. This map you must create during front binding. Looks like this: Map stackMap ->Activity -> Insert user fragment -> add user id to map as key -> replace fragment with post-> add post id to map as value for user key -> replace with user-> add key to map ...... etc. For back trace use map? in opposite order(take id, get data, bind data to fragment).

There are more better for memory than activity instantiation.

P.S: you can organize your stack as you wish not only with key value but as array or linked list or even as array list with stack object entities

once2go
  • 1,452
  • 1
  • 13
  • 21