其实我知道我问的是Android的简单和基本概念。finish()
但是我对这些和onDestroy()
方法有点困惑。这是否会杀死活动并释放与这些活动相关的资源?
我尝试了一个只包含一个活动的简单应用程序。我认为这个概念就像当应用程序运行时,活动将启动。当我们单击后退按钮时,它将完成。我在每个生命周期方法中给出了一些 toast 消息,用于了解内存使用情况。当我单击后退按钮时,它执行onPause()
,onStop()
和onDestroy()
. 我以为这个活动结束了。但是当我再次重新启动应用程序时,它比上次占用了更多的内存。每次我从 Eclipse 运行应用程序或从主屏幕重新启动应用程序时都会发生这种情况。
为什么会这样?我如何才能真正销毁应用程序/活动以释放内存?
我包括我的代码。我只在课堂上给出一条祝酒词。然后内存使用量也在增加。
每次运行应用程序时,分配的大小都会增加,例如:3302744、3442384、3474552
public class myActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Toast.makeText(getBaseContext()," allocated size = " + Debug.getNativeHeapAllocatedSize(), 1).show();
}
}
显现:
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".myActivity "
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
为什么内存每次都在增加?