简体   繁体   English

onDestroy() 或 finish() 是否真的杀死了活动?

[英]Does onDestroy() or finish() actually kill the activity?

Actually I know i am asking about the simple and basic concept of Android.其实我知道我问的是 Android 的简单和基本概念。 But I am a little bit confused about these finish() and onDestroy() methods.但是我对这些finish()onDestroy()方法有点困惑。 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.为了了解 memory 的用法,我在每个生命周期方法中给出了一些祝酒词。 And when I clicked on the back button it executed onPause() , onStop() , and onDestroy() .当我点击后退按钮时,它执行onPause()onStop()onDestroy() I thought this activity finished.我以为这个活动结束了。 But when i relaunched the application again, then it took more memory than the previous time.但是当我再次重新启动应用程序时,它比上次花费了更多的 memory。 This happens every time when I run the app from eclipse or relaunch the application from home screen.每次我从 eclipse 运行应用程序或从主屏幕重新启动应用程序时都会发生这种情况。

Why is it happening?为什么会这样? How can i actually destroy the application / activity to free the memory?我如何才能真正销毁应用程序/活动以释放 memory?


I am including my code.我包括我的代码。 I just give only one toast message inside the class.我只在 class 中给出一条祝酒消息。 Then also memory usage is increasing.然后 memory 的使用量也在增加。

Each time when I run the application the allocated size is increasing like: 3302744, 3442384, 3474552每次运行应用程序时,分配的大小都会增加,例如: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?为什么memory每次都在增加?

The finish() kills the activity and releases memory... unless you have some reference stored that is leaked... for example on methods like onRetainNonConfigurationInstance() finish() 终止活动并释放 memory ... 除非您存储了一些泄露的参考...例如在 onRetainNonConfigurationInstance() 等方法上

When you press the back button what is called is the finish() method that than calls onPause, onStop, onDestroy.当您按下后退按钮时,调用的是finish() 方法,它会调用onPause、onStop、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.不过,在 onDestroy 或 onPause 中显示 toast 并不是一个好主意。 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.顺便说一句,finish() 是您从代码中显式调用的东西,而 onDestroy() 是一个生命周期事件/方法,由于以任何方式完成/破坏活动而被调用。

Finish() will literally finish your activity and if no references are present a GC will recover resources. Finish() 将真正完成您的活动,如果不存在引用,则 GC 将恢复资源。 onDestory() is actually a method that the system will call when it is destroying your activity and you are supposed to implement this function. onDestory() 实际上是系统在销毁您的活动时将调用的方法,您应该实现此 function。 You dont need to worry avout destroying your app, android does it for you.您无需担心会破坏您的应用程序,android 会为您解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM