onDestroy() 或 finish() 是否真的会终止该活动?

发布于 2024-11-09 09:16:20 字数 1418 浏览 0 评论 0原文

其实我知道我在问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>

为什么内存每次都会增加?

Actually I know i am asking about the simple and basic concept of Android. But I am a little bit confused about these finish() and onDestroy() methods.
Whether this will kill the activity and free the resources associated with these activity?

I tried with a simple application which contains only one activity. I thought the concept is like When the application runs, the activity will start. and when we click on back button, it will finish. And I gave some toast message inside each life cycle methods for knowing the memory usage . And when I clicked on the back button it executed onPause(), onStop(), and onDestroy(). I thought this activity finished. But when i relaunched the application again, then it took more memory than the previous time. This happens every time when I run the app from eclipse or relaunch the application from home screen.

Why is it happening? How can i actually destroy the application / activity to free the memory?


I am including my code. I just give only one toast message inside the class. Then also memory usage is increasing.

Each time when I run the application the allocated size is increasing like : 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();      
         }

   }

manifest:

<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>

Why is the memory increasing every time?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

紫竹語嫣☆ 2024-11-16 09:16:20

finish() 会终止活动并释放内存...除非您存储了一些泄漏的引用...例如 onRetainNonConfigurationInstance() 等方法

当您按下后退按钮时,调用的是 finish() 方法调用 onPause、onStop、onDestroy。

The finish() kills the activity and releases memory... unless you have some reference stored that is leaked... for example on methods like onRetainNonConfigurationInstance()

When you press the back button what is called is the finish() method that than calls onPause, onStop, onDestroy.

笑叹一世浮沉 2024-11-16 09:16:20

默认行为是后退按钮将导致 Activity 退出并被销毁。
不过,在 onDestroy 或 onPause 中显示 toast 并不是一个好主意。它将以您不希望发生的方式改变您的活动的生命周期。
使用日志记录代替,这样您就会看到到底发生了什么。
顺便说一句, finish() 是您从代码中显式调用的东西,而 onDestroy() 是一个生命周期事件/方法,它会因以任何方式完成/销毁活动而被调用。

The default behavior is that back button will cause the activity to exit and it'll be destroyed.
Displaying a toast in onDestroy or onPause is not a good idea though. It'll alter the lifecycle the of your activity in a way you don't want it to happen.
Use logging instead, so you'll see what's really happening.
BTW, finish() is something that you call explicitly from your code and onDestroy() is a lifecycle event/method which gets called as a result of finishing/destoying the activity in any way.

哥,最终变帅啦 2024-11-16 09:16:20

Finish() 将真正完成您的活动,如果不存在引用,GC 将回收资源。 onDestory() 实际上是系统在销毁你的 Activity 时会调用的一个方法,你应该实现这个函数。您无需担心会破坏您的应用程序,Android 会为您做到这一点。

Finish() will literally finish your activity and if no references are present a GC will recover resources. onDestory() is actually a method that the system will call when it is destroying your activity and you are supposed to implement this function. You dont need to worry avout destroying your app , android does it for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文