Ayaz Alavi
Ayaz Alavi

Reputation: 4835

How to completely remove activity from stack once finish is called over it?

When I launch app my heap size increases to 10MB on first activity then when I go to next activity my heap size increases to 37 MB. On this second activity I am releasing some memory by bitmap.recycle() on button click so when clicked on particular button heap size decreases to 32MB. Now when I call finish to this second activity my memory size decreases to 16MB so giving a hint that it is not completely removed from stack since original memory size on first activity was 10MB.

Now when i go back to second activity it gives following crash that shows that bitmaps that I have released on button click are not being recreated. I am using same onCreate method on second activity everytime user enters that activity so it should have fetched bitmaps again from xml.

06-26 08:45:39.636: E/AndroidRuntime(6265): java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@41361ef0

What I am missing here? How to completely remove activity from stack once finish is called over it?

I have tried using android:launchMode="singleTop" and android:clearTaskOnLaunch="true" on activity one and android:launchMode="singleTop" android:noHistory="true" on activity two.

any suggestions will be appreciated.

Upvotes: 0

Views: 2420

Answers (2)

Daud Arfin
Daud Arfin

Reputation: 2499

Once finish is get called onDestroy will also get called and entire activity will be cleared but even though you want to try something manually, you can try stack clear or can call garbage collector. For your reference :

// For stack clear
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

// For calling GC
System.runFinalization();
System.exit(0);

Upvotes: 1

Christopher Stock
Christopher Stock

Reputation: 1436

In order to remove an application from the System's application stack, you can kill it's process. Maybe this helps?

/**************************************************************************************
*   Removes this application from the stack by killing it's process.
**************************************************************************************/
public static final void killProcess()
{
    //get current process id
    int processId = android.os.Process.myPid();

    //tell android to kill this process
    android.os.Process.killProcess( processId );
}

Upvotes: 1

Related Questions