堆栈中的活动过多,导致应用程序运行缓慢 [英] Too many Activity in the stack make the app pretty slow

查看:90
本文介绍了堆栈中的活动过多,导致应用程序运行缓慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我创建了一个社交应用.我没有使用片段,项目几乎完成了.我有几个活动,例如UserProfile,Followers,Follows活动.

Recently I created a social app. I didn't use fragment and the project is almost finished. I have several Activities like UserProfile, Followers, Followings activity.

通常情况下,一切正常.但是,如果用户单击UserA UserProfile活动->然后单击A的关注者->选择UserB Userprofile活动->单击B的关注者活动->选择UserC Userprofile活动....

Normally it's just working fine. But if user click UserA UserProfile activity -> and then click A's Followers -> select UserB Userprofile activity -> click B's followers activity -> select UserC Userprofile activity....

在此循环中,该应用程序会变得非常慢,因为它同时打开了太多活动,而后堆栈则保存了所有活动.

In this loop, the app would get pretty slower because it opened too many activities at same time and the back stack hold all of them.

我只是想知道我是否可以针对这种情况进行优化?因为UserProfile活动布局除用户信息内容外将始终相同.即使不同的活动会一个接一个地出现,是否有可能对每个活动都使用Fragment?

I just wonder if there's any optimization I could do for this situation? Because UserProfile activity layout would always same except the user information content. Is that possible to use Fragment for each activity, even though different activities would show up in sequence one by one?

谢谢!

推荐答案

您应该以不同的方式来构造它.堆栈中只能有一个UserProfileActivity.如果堆栈中已经有用户A的UserProfileActivity,并且要显示用户B的UserProfileActivity,只需用Intent.FLAG_ACTIVITY_REORDER_TO_FRONT调用UserProfileActivitystartActivity()并传递一些附加信息以表明Activity应该显示用户B.对您的所有活动都使用相同的概念.

You should architect this in a different way. You should only ever have one UserProfileActivity in the stack. If you already have the UserProfileActivity for User A in the stack, and you want to show the UserProfileActivity for User B, just call startActivity() for UserProfileActivity with Intent.FLAG_ACTIVITY_REORDER_TO_FRONT and pass some extras to indicate that the Activity should show User B. Use the same concept for all of your activities.

为确保后退"按钮导航正常工作,您将需要覆盖onBackPressed()并弄清楚需要显示什么Activity和哪些数据.然后调用startActivity()并设置Intent.FLAG_ACTIVITY_REORDER_TO_FRONT并提供其他功能,以便Activity显示正确的数据.

To make sure that the BACK button navigation works correctly, you will need to override onBackPressed() and figure out what Activity needs to be shown and with what data. Then call startActivity() and also set Intent.FLAG_ACTIVITY_REORDER_TO_FRONT and provide extras so the Activity will show the correct data.

为帮助跟踪您在导航中的位置,您可能想创建一堆存储在static变量中某处的项目.每一项将指示正在显示什么Activity以及带有哪些数据.每次启动新的Activity时,都会将新项目推送到此堆栈上,并且每次用户按BACK键时,都会从堆栈中弹出顶部项目,然后查看其下方的项目以确定Activity开始,并发送其他数据.

To assist in keeping track of where you are in the navigation, you might want to create a stack of items that are stored in a static variable somewhere. Each item would indicate what Activity is being shown and with what data. Every time you launch a new Activity, you push a new item on to this stack, and every time the user presses the BACK key, you pop the top item off the stack and then look at the one underneath it to determine what Activity to start and what data to send in the extras.

使用此方案,用户可以整天单击鼠标,每个Activity的实例将不会超过一个,但是用户仍然可以一直导航.

With this scheme, the user can click around all day long and you will never have more than one instance of each Activity, but the user will still be able to navigate all the way back.

这篇关于堆栈中的活动过多,导致应用程序运行缓慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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