Android Tabs - Navigation between tabs causing memory leak
I finished an application recently and began profiling it for CPU & Memory usage and came across a memory leak:
In a simple 3 tab application where each tab has a distinct function, I have a button on Tab3 which sends the user back to Tab1 along with a messageId. The activity associated with Tab1 catches this extra and displays the message if its present. See below for the button in Tab3:
public void onClick(View arg0) {
Intent notificationIntent = new Intent(getApplicationContext(), TabContainerActivity.class);
notificationIntent.setAction("com.test.notify.MESSAGE");
notificationIntent.putExtra(MessageBean.MESSAGE_ID, messageId);
startActivity(notificationIntent);
}
The tab container TabContainerActivity.class
replicates Extras
to child Actions. It also has logic to capture the Action
sent with the intent and if one is found, it is used to decide which tab to set as current:
if(getIntent().getExtras() != null ) intent.putExtras(getIntent().getExtras());
if(getIntent().getAction().equalsIgnoreCase("com.test.notify.MESSAGE"))
{
tabHost.setCurrentTab(0);
}
else if(getIntent().getAction().equalsIgnoreCase("com.test.notify.SETTINGS"))
{
tabHost.setCurrentTab(1);
}
...etc...
With the above code you can move between Tabs normally, you can also click on a message in Tab3 and have it displayed in full in Tab1 - Great! The code can easily be modified in future to allow other applications to pass MessageID's to the first tabs activity, Wonderfu!
However - This behaviour is now in evidence:
- Click Tab3,
- Select a message
[[ A new intent is sent, opening a new
TabContainerActivity
activity]] - Inside this activity click Tab3,
- Select another message
[[ A new intent is sent, opening ANOTHER new
TabContainerActivity
activity]] - (Repeat 3 & 4 & 5)
Hopefully you can see the problem! You can go back
through each of these activities, pealing them off the stack, but because of the recursive nature of the problem you can continue navigating, creating a new activity with each pass, to the point of OutOfMemory exceptions.
So, to the question;
I want the functionality contained inside Tab1's activity to run whenever the Tab is made visible (In this case, loading a message) so I would like to keep it as an Activity in its own right. Saying that, when navigated to I want it to be the only Tab Activity which is on the stack. How can I achieve this?
Any开发者_如何学JAVA help and discussion would be appreciated!
Use this
public void onClick(View arg0) {
Intent notificationIntent = new Intent(NotifyService.getInstance(), TabContainerActivity.class);
notificationIntent.setAction("com.test.notify.MESSAGE");
notificationIntent.putExtra(MessageBean.MESSAGE_ID, messageId);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(notificationIntent);
}
I think just using .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
to the intent you're using to start the activity again will help you to get the desired result.
精彩评论