[英]Why does my android apps activitie reopen when I close my app
I have been trying to solve this problem for a while now and I am sure there is a way to solve this error/problem but I haven't found that yet.我已经尝试解决这个问题一段时间了,我相信有办法解决这个错误/问题,但我还没有找到。
The problem I am having is once I exit the app via the back button or home button it closes but then reopens one of my activities which was opened before.我遇到的问题是,一旦我通过后退按钮或主页按钮退出应用程序,它就会关闭,然后重新打开我之前打开的一项活动。
Do I need to close my activities once I press one of those buttons and how would I go around doing that.一旦我按下这些按钮之一,我是否需要关闭我的活动,我将如何去做。
Here is the code for my main activity that opens my other activity...这是我的主要活动的代码,用于打开我的其他活动...
package com.togdem.tunnels;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.Toast;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class TunnelsActivity extends Activity
{
int vo = 2;
String mla = "";
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView p_btn = (ImageView) findViewById(R.id.imageView1);
p_btn.setOnTouchListener(new OnTouchListener()
{
public boolean onTouch(View view, MotionEvent event)
{
Intent myIntent2 = new Intent(view.getContext(), Cgt.class);
startActivity(myIntent2);
return false;
}
});
SeekBar skb = (SeekBar) findViewById(R.id.seekBar1);
try {
InputStream in = openFileInput("sa.txt");
if (in != null) {
InputStreamReader input = new InputStreamReader(in);
BufferedReader buffreader = new BufferedReader(input);
mla = "";
String line = new String();
line = buffreader.readLine();
mla += line;
Toast msg = Toast.makeText(getBaseContext(), "Loaded Saved Data", Toast.LENGTH_LONG);
msg.show();
in.close();
skb.setProgress(Integer.parseInt(mla.toString()));
}
else{}
} catch(Exception e){}
skb.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
public void onProgressChanged(SeekBar event, int value, boolean sb)
{
vo = value;
}
public void onStartTrackingTouch(SeekBar event)
{
}
public void onStopTrackingTouch(SeekBar event)
{
try
{
OutputStreamWriter out = new OutputStreamWriter(openFileOutput("sa.txt", 0));
out.write(Integer.toString(vo));
out.close();
} catch (IOException e)
{
}
Toast msg = Toast.makeText(getBaseContext(), "Set Sensitivity to: " + Integer.toString(vo), Toast.LENGTH_LONG);
msg.show();
}
});
}
}
And here is the code for the activity I am trying to open...这是我尝试打开的活动的代码...
package com.togdem.tunnels;
import java.io.OutputStreamWriter;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
public class Cgt extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView( R.layout.cgame );
ImageView btn1 = (ImageView) findViewById(R.id.btn_c);
ImageView btn2 = (ImageView) findViewById(R.id.btn_h);
btn1.setOnTouchListener(new OnTouchListener()
{
public boolean onTouch(View view, MotionEvent event)
{
try
{
OutputStreamWriter out = new OutputStreamWriter(openFileOutput("spd.txt", 0));
out.write("4");
out.close();
} catch (java.io.IOException e) {}
Intent myIntent = new Intent(view.getContext(), TunnelsSpA.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
startActivity(myIntent);
finish();
return false;
}
});
btn2.setOnTouchListener(new OnTouchListener()
{
public boolean onTouch(View view, MotionEvent event)
{
try
{
OutputStreamWriter out = new OutputStreamWriter(openFileOutput("spd.txt", 0));
out.write("6");
out.close();
} catch (java.io.IOException e) {}
Intent myIntent = new Intent(view.getContext(), TunnelsSpA.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
startActivity(myIntent);
finish();
return false;
}
});
}
public boolean onKeyDown(int KeyCode, KeyEvent event)
{
if (KeyCode == KeyEvent.KEYCODE_BACK);
{
onStop();
}
if (KeyCode == KeyEvent.KEYCODE_HOME);
{
onStop();
}
return super.onKeyDown(KeyCode, event);
}
protected void onStop()
{
finish();
Log.d("Stopping...", "Stopping the activity");
}
}
Thanks for any help you can give me,感谢你给与我的帮助,
Your Application is in Stopped State
...where the Activity (Your app Activity) is hidden by Another ForeGround Activity...您的应用程序处于
Stopped State
......活动(您的应用活动)被另一个前台活动隐藏......
Either System
kills this Activity
when there is a need of memory .. or you can kill it by calling finish()
or finishActivity()
当需要内存时,
System
终止此Activity
.. 或者您可以通过调用finish()
或finishActivity()
它
You seem to have trouble with some basic Android behaviour.您似乎对一些基本的 Android 行为有问题。
First of all, when the user presses the HOME button, he isn't exiting your application , he is just putting your application in the background so that he can go do something else.首先,当用户按下 HOME 按钮时,他并没有退出您的应用程序,他只是将您的应用程序置于后台,以便他可以做其他事情。 He doesn't expect your application to exit if he does this.
如果他这样做,他不希望您的应用程序退出。 In fact, when he returns to your application he expects to find it in exactly the same condition as when he left it.
事实上,当他返回您的应用程序时,他希望找到与他离开时完全相同的状态。 This is standard Android behaviour and you shouldn't change that unless you have a really good reason to.
这是标准的 Android 行为,除非您有充分的理由,否则不应更改它。
Secondly, when the user presses the BACK button, Android automatically finishes the current activity and returns to the previous activity in the activity stack.其次,当用户按下 BACK 按钮时,Android 会自动完成当前的 Activity 并返回到 Activity 堆栈中的上一个 Activity。 This is standard Android behaviour and you don't need to write anything special to make this happen.
这是标准的 Android 行为,您无需编写任何特殊内容即可实现此目的。
From looking at your code you have activity TunnelsActivity
that starts activity Cgt
.通过查看您的代码,您有活动
TunnelsActivity
启动活动Cgt
。 TunnelsActivity
does not call finish()
when it starts Cgt
so the activity stack will have TunnelsActivity
on the bottom and then Cgt
on the top when that activity is running. TunnelsActivity
不调用finish()
当它开始Cgt
所以活动堆栈将有TunnelsActivity
底部,然后Cgt
上时活动运行的顶部。 in Cgt
you are catching the BACK button and the HOME button and calling onStop()
.在
Cgt
您正在捕捉 BACK 按钮和 HOME 按钮并调用onStop()
。 This is definitely wrong.这绝对是错误的。 You should never call
onStop()
.你永远不应该调用
onStop()
。 onStop()
is called by Android as part of the Activity lifecycle in certain situations.在某些情况下,作为 Activity 生命周期的一部分,Android 会调用
onStop()
。 Calling onStop()
will not make your activity go away nor will it make your application exit.调用
onStop()
不会使您的活动消失,也不会使您的应用程序退出。 All it will do is confuse Android.它只会混淆Android。 Also, when Android calls
onStop()
it expects the code to call through to super.onStop()
which your code isn't doing.此外,当 Android 调用
onStop()
它希望代码调用super.onStop()
,而您的代码没有这样做。 This will cause the code to throw an Exception, which may be why you are seeing your activities getting restarted.这将导致代码抛出异常,这可能就是您看到活动重新启动的原因。
I suggest removing your onStop()
method and removing your onKeyDown()
method.我建议删除您的
onStop()
方法并删除您的onKeyDown()
方法。 That may solve all your problems.那可能会解决你所有的问题。
just use for quit the application System.exit(0);仅用于退出应用程序 System.exit(0);
not use finish();不使用完成();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.