简体   繁体   English

android - finish()关闭app而不是活动

[英]android - finish() closes app instead of the activity

I'm a begginer android programmer, and I seem to have a problem: I open new activitys, with 我是一个初学的android程序员,我似乎有一个问题:我打开新的活动,用

Intent newGameIntent = new Intent(actionName);
startActivity(newGameIntent);

and everythings works fine, the activity opens. 并且每件事都很好,活动开始了。 but when i call finish() it doesnt goes to the previus activity, it just closes the app(no errors or other log messeges) 但是,当我调用finish()时,它不会进入previus活动,它只是关闭应用程序(没有错误或其他日志消息)

does anyone have an idea why is it happening? 有没有人知道它为什么会发生? Thanks for your time! 谢谢你的时间!

by request, here is more of the code(of the stuff that i might have totally screwd up): first activity: 根据要求,这里有更多的代码(我可能完全搞砸了的东西):第一个活动:

@Override
protected void onStop() {
    super.onStop(); 
    SplashScreen.sounds.releasSounds();
    finish();
}
@Override
protected void onPause() {
    super.onPause();
    pauseActivity();
}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    gameLoop.resumeThread();
    SplashScreen.sounds.resumeSounds();
}

private void pauseActivity() {
    gameLoop.pauseThread();
    SplashScreen.sounds.pauseBck();
}

and the first activity calling the seconds activity 和第一个调用秒活动的活动

Intent newGameIntent = new Intent("com.YuvalApps.menus.NEWGAMEMENU");
    startActivity(newGameIntent);

and for the seconds activity 并为秒活动

    @Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    finish();

}

When one activity was in background, The android system will invoke "onStop" method, But you invoke the method "finish" in method "onStop", So if you jump to another activity, the previous activity will destroyed by "onStop". 当一个活动在后台时,android系统将调用“onStop”方法,但你在方法“onStop”中调用方法“finish”,所以如果你跳转到另一个活动,前一个活动将被“onStop”破坏。 You should remove "finish" in "onStop" method. 您应该删除“onStop”方法中的“完成”。

如果你调用finish()来关闭当前活动并返回到前一个活动,只需调用onBackPressed()而不是finish()。它将带你回到之前的活动。

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

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