安卓:内存不足错误和backstack [英] Android: OutOfMemory error and the backstack

查看:134
本文介绍了安卓:内存不足错误和backstack的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的片材再presents工作流应用程序中的这个问题是关于

the following sheet represents the working flow in the application this question is about.

我遇到了问题,内存不足的错误,主要是因为用户能够从活动B切换到活动ð多次(他们表示每次尝试不同的内容),而previous活动遭到破坏。这导致进入从而导致内存不足错误非常大的backstack。

I ran into problems with OutOfMemory Errors, mostly because users were able to switch from activity B to activity D multiple times (They are showing different content for every attempt), without the previous activity being destroyed. This led into a very large backstack resulting in an OutOfMemory error.

要避免这种情况,我也跟着父母activites添加到清单,并创造新的backstacks与TaskStackBuilder类推荐:

To avoid this, I followed the recommendation to add parent activites to the manifest and create new backstacks with the TaskStackBuilder class:

void openDFromB()
{
    Intent i = new Intent(this, ActivityD.class);
    TaskStackBuilder.create(this)
        .addParentStack(ActivityD.class)
        .addNextIntent(i)
        .startActivities();
}

如果用户现在从切换和活动B至D,MainMenu的,A,B被破坏,新的Backstack(MainMenu的,C,D)的创建。 这解决了我的记忆问题的API级别更高的10,但不幸的是,TaskStackBuilder不会创建backstacks的设备pre API 11。

If a user now switches from activity B to D, MainMenu, A and B are destroyed and a new Backstack (MainMenu, C, D) is created. This solves my memory problems for api level greater 10, but unfortunately the TaskStackBuilder does not create backstacks for devices pre api 11.

任何想法,该怎么做,以避免用户叠加的活动之前,API 11无限量?这甚至可能或者我应该尝试释放尽可能多的资源,尽可能在OOM的onPause之前获得堆叠的活动量最多?

Any idea what to do in order to avoid users stacking infinite amounts of activities prior api 11? Is this even possible or should I try to free as many resources as possible in onPause to gain a maximum amount of stacked activities before OoM?

在此先感谢!

推荐答案

Zabri回答是好,但我会提出另一种可能是更好的: 你不收的每一次活动,但是当您打开之前已经打开了新的活动,你告诉系统,它可以清除回来的路上并重新打开一个,当然是一个新的意图和所有你需要的新数据

Zabri answer is good, but i would propose another probably is better: You dont close every activity, but when you are opening a new activity that has been open before, you tell the system that it can clear the way back and reopen that one, of course with a new intent and all the new data that you need.

所以,如果你做的A - B - ð - B - D - B,而不是具有很大的堆栈,你将有

So, if you do A - B - D - B - D - B, instead of having that big stack,you will have

  1. 系统 - B
  2. A - B - ð
  3. 系统 - B
  4. A - B - ð
  5. 系统 - B

所以,如果你可做C - ð - B - ð - B - D,你的筹码将是

So, if you do C - D - B - D - B - D, your stack will be

  1. ç - ð
  2. Ç - ð - B
  3. ç - ð
  4. Ç - ð - B
  5. ç - ð

等。 通过这种方式,我认为堆栈实际上反映了用户能有什么想法,也是导航很逻辑,你甚至都不需要captre后退按钮具有导航决策意识,(当然,如果你能做到这一点你想要一个更具体的用户体验)

and so on. In this way, i think the stack actually reflects what the user can have in mind, and also navigation is very logic, you don't even need to captre back button to have the navigation making sense, (of course you can do it if you want a more specific user experience)

的code键实现这一点,是: 创建一个函数,接受一个类(像ActivityD.class),还有你创建的意图,与标志 Intent.FLAG_ACTIVITY_CLEAR_TOP Intent.FLAG_ACTIVITY_NEW_TASK ,然后当你需要打开一个活动,就调用这个函数。你不关心,如果活动是在栈或没有。如果它不是,则正常创建

The code to achieve this, is: create a function that receives a class (something like ActivityD.class), there you create an intent, with the flags Intent.FLAG_ACTIVITY_CLEAR_TOP and Intent.FLAG_ACTIVITY_NEW_TASK and then when you need to open an activity, just call this function. You dont care about if the activity is in the stack or not. If it isnt,it is created normally.

 protected void startActivity(Class<?> clase) {
    final Intent i = new Intent(this, clase);
    int flags = Intent.FLAG_ACTIVITY_CLEAR_TOP;
    flags |= Intent.FLAG_ACTIVITY_NEW_TASK;
    i.setFlags(flags);
    startActivity(i);
}


startActivity(ActivityD.class);

这篇关于安卓:内存不足错误和backstack的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆