At one point in my app, the user selects an image from a Gallery widget embedded into a selection activity. After selection, the next Activity loads.
The next activity uses more bitmaps. In an attempt to keep the heap size down, I am trying to destroy the selection activity after it is no longer needed. Using DDMS, I noticed that the activity was not being killed when I called finish(). I then proceeded to strip the activity down to the bare minimum and the activity still doesn’t seem to finish. Below Shows what I’m left with
public class ImageSelectActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.selectionpage);
}
public void buttonClick(View v){
Intent intent = new Intent(ImageSelectActivity.this, MainActivity.class);
intent.putExtra("imageID", 0);
startActivity(intent);
finish();
}
After finding the ImageSelectActivity still alive on DDMS. I looked at the path to GC roots and got the following:
Java.lang.Thread
+- android.app.ContextImpl
+- app.myapplication.ImageSelectActivity
Another thing, If I press back the application closes. I am completely lost. Any ideas?
That’s how Android works. When you call “finish()” is essentially quits the Activity right there. Android will not pull it from the list of running processes until it absolutely has to for performance reasons (why waste resources dumping resources that isn’t needed yet?). It will sit in memory until the memory is needed. However, it’s as good as dead.
The one exception to this (that I can think of right now) is if you have a thread running. Threads will continuously run for forever or until they end, but they will continue to run after finish() is called.
On your last thing, if you’re in the first
Activityof your application, pressing the back button should close it (akin to callingfinish()).