I have created simple app with two acitivities, in both activities there a button which links to another activity.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.two);
Button btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (intent != null) intent = null;
intent = new Intent(Two.this,MainActivity.class);
startActivity(intent);
}
In both activity there is same code. When i press Button and press Cause GC in DDMS, allocated is always growing by 25kb is it normal?
I wouldn't call it a memory leak but poor concept. What you are doing is putting activities on the stack which, of course, takes up more and more memory. If you want to just open the previous activity (thus not creating a new one each time you call startActivity(...)
), use this:
Intent intent = new Intent(Two.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
If you want to know more about tasks and the back stack, visit this guide.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments