Why does heap memory increase when re-launching an activity?

前端 未结 5 1084
悲哀的现实
悲哀的现实 2020-12-05 16:00

This question concerns memory in Android.

My method:

I have two activites, A and B. From A, I launch B like this:

Intent i =         


        
5条回答
  •  一生所求
    2020-12-05 16:27

    If you have 5 instances of activity B, then you are not managing the activity stack correctly. I find the best way to check it is with the CLI command:

    adb shell dumpsys meminfo 'your apps package name'

    I had a similar problem in a two activity project when I switched between them. Every time I switched I got a new instance on the stack as revealed by the above command. I then set the flags for the launched activities to FLAG_ACTIVITY_REORDER_TO_FRONT with code like:

    Intent i = new Intent("com.you.yourActivityB");
    i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
    startActivity(i);
    

    Once I'd done this, then the adb shell command did not show more instances of my two activities when I switched between them

提交回复
热议问题