[英]onDestroy never called
I have updated the latest Android Studio and created a new Blank Project. 我已经更新了最新的Android Studio并创建了一个新的Blank Project。 I added the following code to the MainActivity.java file, but onDestroy() is never called.
我将以下代码添加到MainActivity.java文件中,但从未调用过onDestroy()。 Is there any way to get destroy event?
有什么办法可以破坏事件吗?
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}
AndroidManifest.xml AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.edevshop.destroy">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
onDestroy() is called only when system is low on resources(memory, cpu time and so on) and makes a decision to kill your activity/application or when somebody calls finish() on your activity. 仅当系统资源不足(内存,CPU时间等)并决定终止您的活动/应用程序或有人对您的活动调用finish()时,才会调用onDestroy()。
So, to test your code() you can make a test button, that will call finish() on your activity. 因此,要测试您的code(),您可以创建一个测试按钮,该按钮将在您的活动上调用finish()。
Also, I believe you don't need to call all this stuff in onDestroy() until adap is not a critical resource. 另外,我相信您不需要在adDe不是关键资源之前在onDestroy()中调用所有这些东西。 And even in that case android system has mechanisms to properly dispose them.
即使在那种情况下,android系统也具有适当处置它们的机制。
just check it out and modify your onDestroy()
method as below: 只需将其签出并修改您的
onDestroy()
方法,如下所示:
@Override
protected void onDestroy() {
Log.d("TAG", "Yay.. onDestroy called!");
super.onDestroy();
}
Now... Run App > Open LOGCAT > close App (By pressing BACK button) You'll see a LOG. 现在...运行应用程序>打开LOGCAT>关闭应用程序(按BACK按钮),您将看到一个日志。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.