2016-08-16 9 views
0

戻るボタンを押したときに終了するようにアプリケーションを実装しました。私がアプリを終了すると、通常、アプリが正常に終了する前に最近のアプリが表示されている途中で途切れてしまうことがあります。この問題の原因は何ですか?ここに私のコードです。戻るボタンが押されたときにジャンプが終了する

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

このコードは問題を追跡するのには十分ではないと思います。戻るボタンをクリックすると、あなたの 'アジリティ'に何か他のものが進行しているようですか? –

+0

_ "不連続な出口" _を定義してください。 –

+0

最近のアプリリストを表示しているうちに、アプリの途中がフリーズしてしまい、最終的に終了します。 – HM88

答えて

0

OS /デバイスが遅くなければなりません。

ここに投稿していない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(); So:

@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(); 

} 
関連する問題