2016-08-16 154 views
0

當按下後退按鈕時,我已經實現了我的應用程序退出。當我退出應用程序時,我通常會在應用程序正常退出之前得到最近應用程序顯示的波濤洶涌的退出。什麼可能導致這個問題?這是我的代碼。按下後退按鈕時出現Ch 0123退出

public class MainActivity extends AppCompatActivity implements View.OnClickListener { 
private Button discover,why,enterprise,business,services,office,inquire,connect; 
TextView offer; 
Intent p; 
private static long back_pressed_time; 
private static long PERIOD = 2000; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    offer = (TextView)findViewById(R.id.offer); 
    discover = (Button) findViewById(R.id.discover_us); 
    why = (Button) findViewById(R.id.why_us); 
    enterprise = (Button) findViewById(R.id.enterprise_soln); 
    business = (Button) findViewById(R.id.business_soln); 
    services = (Button) findViewById(R.id.services); 
    office = (Button) findViewById(R.id.office_automation); 
    inquire = (Button) findViewById(R.id.inquire); 
    connect = (Button) findViewById(R.id.connect); 
} 

@Override 
public void onClick(View v) { 
    switch (v.getId()) 
    { 
     case R.id.discover_us: 
      p = new Intent(this,DiscoverUs.class); 
      startActivity(p); 
      break; 
     case R.id.why_us: 
      p = new Intent(this,WhyTechbiz.class); 
      startActivity(p); 
      break; 
     case R.id.office_automation: 
      p = new Intent(this,Office_Automation.class); 
      startActivity(p); 
      break; 
     case R.id.services: 
      p = new Intent(this,Services.class); 
      startActivity(p); 
      break; 

     case R.id.enterprise_soln: 
      p = new Intent(this,Enterprise_Soln.class); 
      startActivity(p); 
      break; 
     case R.id.business_soln: 
      p = new Intent(this,Business_Soln.class); 
      startActivity(p); 
      break; 
     case R.id.inquire: 
      p = new Intent(this,Inquire.class); 
      startActivity(p); 
      break; 
     case R.id.connect: 
      p = new Intent(this,Connect.class); 
      startActivity(p); 
      break; 
    } 
    overridePendingTransition(R.anim.fade_in, R.anim.fade_out); 
} 


@Override 
public void onBackPressed() { 
    if (back_pressed_time + PERIOD > System.currentTimeMillis()) { 
    super.onBackPressed(); 
} else { 
    back_pressed_time = System.currentTimeMillis(); 
    Toast.makeText(MainActivity.this, "Press once again to exit!", Toast.LENGTH_SHORT).show(); 
} 
} 
+1

我不認爲這段代碼是足以追查你的問題。當你點擊「返回」按鈕時,你的'Acitivity'中是否還有其他事情? –

+0

請定義_「波濤洶涌的出口」_。 –

+0

意思是它的意思是凍結應用程序的中間關閉,同時顯示最近的應用程序列表,然後最終退出 – HM88

回答

0

你的代碼看起來不錯,你的OS/Device必須很慢。

如果還有其他的代碼從你沒有在這裏發佈的onBackPressed()中執行,那可能是因爲別的,一切看起來都不錯。

+0

編輯我的代碼看一看。不能是我的手機,因爲它有3演出的RAM加上我試過它在不同的手機 – HM88

1

當你完成活動只是調用也super.onBackPressed()

1

請試試這個:

@Override 
public void onBackPressed() { 

    if (back_pressed_time + PERIOD > System.currentTimeMillis()) { 
     super.onBackPressed(); 
    } else { 
     back_pressed_time = System.currentTimeMillis(); 
     Toast.makeText(MainActivity.this, "Press once again to exit!", Toast.LENGTH_SHORT).show(); 
    } 
} 

調用finish()是不是一個好的選擇退出的活動,你可以只使用super.onBackPressed()

+0

仍然沒有區別 – HM88

0

通過在我的應用程序主題中將windowIsTranslucent設置爲false來解決此問題。

0

首先你應該初始化back_pressed_time並設置PERIOD = 3000最後一步這一行應該是頂部
back_pressed_time = System.currentTimeMillis();因此:

@Override 
public void onBackPressed() { 

back_pressed_time = System.currentTimeMillis(); 
    if (back_pressed_time + PERIOD > System.currentTimeMillis()) this.finish(); 
    else Toast.makeText(MainActivity.this, "Press once again to exit!", Toast.LENGTH_SHORT).show(); 

}