Too many Activity in the stack make the app pretty slow

前端 未结 1 1823
别那么骄傲
别那么骄傲 2021-01-25 21:05

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.

相关标签:
1条回答
  • 2021-01-25 21:45

    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.

    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.

    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.

    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.

    0 讨论(0)
提交回复
热议问题