Android 如何确保在用户退出时调用onDestroy方法
我在代码中调用onDestroy()方法时遇到问题。 我的代码的目的是在用户使用屏幕底部的按钮退出应用程序时编写两个txt文件。这可以挽救他们的进步。 然后将其加载到OnCreate()方法中 但是,不会调用OnDestroy()方法 当按下手机上的“退出”按钮时,是否存在强制执行此操作的情况? 代码的主体没有包括在内,因为它对我的问题不重要。非常感谢。 这是我的密码Android 如何确保在用户退出时调用onDestroy方法,android,Android,我在代码中调用onDestroy()方法时遇到问题。 我的代码的目的是在用户使用屏幕底部的按钮退出应用程序时编写两个txt文件。这可以挽救他们的进步。 然后将其加载到OnCreate()方法中 但是,不会调用OnDestroy()方法 当按下手机上的“退出”按钮时,是否存在强制执行此操作的情况? 代码的主体没有包括在内,因为它对我的问题不重要。非常感谢。 这是我的密码 protected void onCreate(Bundle savedInstanceState) { super.o
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_card_question);
try{
FileInputStream fis = openFileInput("correct.txt");
//InputStreamReader isr = new InputStreamReader(fIn);
StringBuffer sb= new StringBuffer();
BufferedReader br= new BufferedReader(new InputStreamReader(fis));
String strLine = null;
int x=0;
while((strLine=br.readLine())!=null){
arrayCorrect[x]=strLine;
x++;
}
//Log.i("File Reading stuff", "success = " + "hell");
} catch (IOException ioe)
{//ioe.printStackTrace();
}
try{
FileInputStream fis = openFileInput("incorrect.txt");
//InputStreamReader isr = new InputStreamReader(fIn);
StringBuffer sb= new StringBuffer();
BufferedReader br= new BufferedReader(new InputStreamReader(fis));
String strLine = null;
int x=0;
while((strLine=br.readLine())!=null){
arrayIncorrect[x]=strLine;
x++;
}
//Log.i("File Reading stuff", "success = " + "hell");
} catch (IOException ioe)
{//ioe.printStackTrace();
}
@Override
public void onDestroy() {
super.onDestroy(); // Always call the superclass
try{
FileOutputStream fOut = openFileOutput("correct.txt",
MODE_WORLD_READABLE);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
// Write the string to the file
for(int x=0; x<arrayCorrect.length; x++) {
osw.write(arrayCorrect[x] + "\n");
}
/* ensure that everything is
* really written out and close */
osw.flush();
osw.close();
}
catch(IOException ioe){
ioe.printStackTrace();
}
try{
FileOutputStream fOut = openFileOutput("incorrect.txt",
MODE_WORLD_READABLE);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
// Write the string to the file
for(int x=0; x<arrayIncorrect.length; x++) {
osw.write(arrayIncorrect[x] + "\n");
}
/* ensure that everything is
* really written out and close */
osw.flush();
osw.close();
}
catch(IOException ioe){
ioe.printStackTrace();
}
// startActivity(new Intent(getApplication(),second.class));
}
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u card\u问题);
试一试{
FileInputStream fis=openFileInput(“correct.txt”);
//InputStreamReader isr=新的InputStreamReader(fIn);
StringBuffer sb=新的StringBuffer();
BufferedReader br=新的BufferedReader(新的InputStreamReader(fis));
字符串strLine=null;
int x=0;
而((strLine=br.readLine())!=null){
arrayCorrect[x]=斯特林;
x++;
}
//Log.i(“文件读取材料”,“success=“+”hell”);
}捕获(ioe异常ioe)
{//ioe.printStackTrace();
}
试一试{
FileInputStream fis=openFileInput(“不正确的.txt”);
//InputStreamReader isr=新的InputStreamReader(fIn);
StringBuffer sb=新的StringBuffer();
BufferedReader br=新的BufferedReader(新的InputStreamReader(fis));
字符串strLine=null;
int x=0;
而((strLine=br.readLine())!=null){
arrayIncorrect[x]=strLine;
x++;
}
//Log.i(“文件读取材料”,“success=“+”hell”);
}捕获(ioe异常ioe)
{//ioe.printStackTrace();
}
@凌驾
公共空间{
super.ondestory();//始终调用超类
试一试{
FileOutputStream fOut=openFileOutput(“correct.txt”,
模式(世界可读);
OutputStreamWriter osw=新的OutputStreamWriter(fOut);
//将字符串写入文件
对于(int x=0;x您不应该将该代码放在onDestroy方法中。您应该在onPause方法上启动后台服务。在onStop()上执行它。
,是的,它将被多次调用,但是onDestroy()
最终会被调用,但并不总是这样,但是是的,当活动被销毁时会调用它们。在那里记录值以检查它们是否被触发。我猜在super.ondestory()之后编写的任何代码从不调用,因为onDestroy将完全完成应用程序。之后调用的任何内容都将被忽略。当按下退出按钮时,不会调用onDestroy
生命周期,因为您的活动未被破坏。onStop
/onResume
是您应该针对您的情况处理的回调。