Possible bug on TabActivity (Memory Leak)

123 views
Skip to first unread message

Carlos Eduardo Rodrigues de Almeida

unread,
May 31, 2011, 9:35:32 AM5/31/11
to android-platform
Hello,

we were developing an application and we were having a memory leak
while changing the device orientation. I wrote a small code that
reproduces the error:

My project has two classes: TabTest and MyTabActivity.

Here is the code for TabTest:

package edeploy.tabtest;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;

public class TabTest extends TabActivity {
private TabHost tabHost;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

tabHost = getTabHost();

Intent intent = new Intent(getApplicationContext(),
MyTabActivity.class);

tabHost.addTab(tabHost.newTabSpec("Test1").setIndicator("Test1").setContent(intent));

tabHost.addTab(tabHost.newTabSpec("TAB_2").setIndicator("Test2").setContent(intent));
}
}

Here is the code for MyTabActivity:

package edeploy.tabtest;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class MyTabActivity extends Activity {
private TextView myTextView;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);

myTextView = (TextView)findViewById(R.id.test_tab);
myTextView.setText("teste");

MyTabActivity mta =
(MyTabActivity)getLastNonConfigurationInstance();
if(mta != null) {
//Do something
}
}

@Override
public Object onRetainNonConfigurationInstance() {
return this;
}
}


When I change the device orientation, the method
onRetainNonConfigurationInstance returns the old instance of my the
MyTabActivity object. Then Android created a new TabTest object and a
new MyTabActivity. In the MyTabActivity I use the old instance to get
any information I want.

My MyTabActivity object has a reference to the TabTest object through
the parent attribute. The TabTest object has a HashMap
attribute(mLastNonConfigurationChildInstances) that has all the
objects returned by onRetainNonConfigurationInstance() of each tab.

The problem is that I return the old Activity in the
onRetainNonConfigurationInstance() and the
mLastNonConfigurationChildInstances attribute is never cleared so I
always have a reference to the old activities.

At the first time there is one MyTabActivity that has a reference to
TabTest in the parent attribute. When I rotate the device the first
MyTabActivity is destroyed and a second one is created. The second
MyTabActivity has a reference to the new TabTest in the parent and the
new TabTest parent has a reference to the old MyTabActivity in the
mLastNonConfigurationChildInstances attribute. If I keep rotating I
have a reference to all activities created and I´ll get a
OutOfMemoryException.

Am I missing something of this is a bug on the TabActivity code?

Thank you

Carlos Eduardo Rodrigues de Almeida

unread,
Jun 6, 2011, 4:04:46 PM6/6/11
to android-platform
Good day,

no comments? Can I post a bug about it?

Thank you
--
Eduardo Almeida




--
You received this message because you are subscribed to the Google Groups "android-platform" group.
To post to this group, send email to android-...@googlegroups.com.
To unsubscribe from this group, send email to android-platfo...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-platform?hl=en.


Reply all
Reply to author
Forward
0 new messages