0
我无法在代码中调用onDestroy()方法。 我的代码的目的是当用户通过使用屏幕底部的按钮退出应用程序时编写两个txt文件。这可以节省他们的进展。 然后将它加载到OnCreate()方法 然而,OnDestroy()方法不会被调用 有没有办法在手机上按下退出按钮时发生这种情况? 代码的主体不包括在内,因为它对我的问题并不重要。谢谢。 这里是我的代码如何确保在用户退出时调用onDestroy方法
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));
}
在'onStop()'做,它会被称为soo很多次,但'onDestroy()'将最终被调用,但并不总是,但是当它们被活动破坏时被调用。在那里检查值,检查它们是否被触发 – Elltz 2015-02-10 00:23:30
我猜测在super.onDestroy()之后编写的任何代码都不会被调用,因为onDestroy完全完成了应用程序。任何之后调用的内容都会被忽略。 – 2015-02-10 00:26:06
当按下退出按钮时,不会调用'onDestroy'生命周期,因为您的活动未被破坏。 'onStop' /'onResume'是你应该为你的情况处理的回调。 – alijandro 2015-02-10 00:27:31