2014-01-16 2 views
0

내 주요 활동에서 하드웨어 뒤로 버튼을 눌러 앱을 종료하라는 알림을받습니다. 이것은 사용자가 죽을 때를 제외하고는 대부분의 환경에서 작동합니다. 사용자가 죽으면 GameOverActivity로 이동합니다. 사용자가이 액티비티에서 뒤로 버튼을 누른 다음 주요 액티비티를 두 번 뒤로 누르면 액티비티가 다시 시작됩니다. 여기 코드가 있습니다, 나는 게임에서 뒤로 버튼에 finish()를 선언했지만, 도움이되지는 않습니다.눌려진 하드웨어 버튼을 두 번 닫으면 이전 활동으로 돌아갑니다.

MainScreen 다시 종료하는 방법

@Override 
    public void onBackPressed() { 

     if (doubleBackToExitPressedOnce) { 
      super.onBackPressed(); 
      return; 
     } 
     this.doubleBackToExitPressedOnce = true; 
     Toast.makeText(this, "Press again to quit", Toast.LENGTH_SHORT).show(); 
     new Handler().postDelayed(new Runnable() { 
      @Override 
      public void run() { 
       doubleBackToExitPressedOnce = false; 
      } 
     }, 2000); 
    } 

GameOverActivity 코드 :

if (weight.getBounds().intersect(player.getBounds())) { 
       player.setTouched(false); 
       Intent gameOverIntent = new Intent(this.getContext(), GameOverActivity.class); 
       this.getContext().startActivity(gameOverIntent); 
       ((Activity) getContext()).finish(); 
      } 
+0

대신 즉시 시작 활동 후 MainScreen에서 finish()를 호출해야합니다; 시도해보십시오 – jyomin

+0

아무런 변화가 없었습니다 –

답변

0

나는 당신을 생각 : 여기

backButton.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       Intent mainScreenActivityIntent = new Intent(GameOverActivity.this, MainScreenActivity.class); 
       startActivity(mainScreenActivityIntent); 
       finish(); 
      } 

     }); 
    } 

    @Override 
    public void onBackPressed() { 
     Intent mainScreenActivityIntent = new Intent(GameOverActivity.this, MainScreenActivity.class); 
     mainScreenActivityIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     startActivity(mainScreenActivityIntent); 
     finish(); 
    } 

이 GameOverActivity의 충돌에 대한 논리 때문에 창조 Activity life cycle에 대해 약간의 혼란이있을 수 있습니다. GameOverActivity을 다시 누르면 mainScreenActivity이라는 새로운 인스턴스가 생성되어 문제가 발생합니다.

하고 GameOverActivity onbackpressed 호출

당신은 새로운 활동을 시작하고 당신이 onBackPressed에서 시작 활동을 완료하는 데 필요한 코드를 추가, 현재 활동을 마무리하는

@Override 
public void onBackPressed() 
{ 
    super.onBackPressed(); 
    // and dont start a new activity as you are stacking MainActivity instances 
    finish() 
} 
+0

이걸로 바꿨지 만 더 이상 MainScreenActivity로 이동하지 않습니다. MainScreen이 생성되지 않습니다. –

+0

메인 -> 게임 -> 게임 오버가있는 경우 게임에 nohHistory 플래그를 추가하면 Gameover 활동에서 뒤로 버튼을 무시하지 않아도됩니다. –

0

을 super.onBackPressed.

0

당신은 YourActivityName.this.finish()를 사용) (마무리의 GameOverActivity

+0

GameOverActivity가 MainScreen에서 생성되지 않습니다. MainScreen은 GameActivity를 시작하여 GameOverActivity를 만듭니다. 게시물에 GameOverActivity를 시작하기위한 로직을 추가하겠습니다. –

관련 문제