【问题标题】:Choppy exit when back button pressed按下后退按钮时断断续续地退出
【发布时间】:2016-08-16 07:11:14
【问题描述】:

我已实现我的应用程序在按下后退按钮时退出。当我退出应用程序时,我通常会在应用程序正确退出之前显示最近的应用程序的断断续续的退出。什么可能导致此问题?这是我的代码。

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

【问题讨论】:

  • 我认为这段代码不足以追踪您的问题。当您单击“返回”按钮时,您的 Acitivity 中是否还有其他内容?
  • 请定义“断断续续的出口”
  • 断断续续的意思是它在应用程序关闭过程中冻结,同时显示最近的应用程序列表,然后最终退出

标签: android activity-finish onbackpressed


【解决方案1】:

当您完成活动时,也只需致电super.onBackPressed()

【讨论】:

    【解决方案2】:

    请试试这个:

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

    【讨论】:

    • 还是没有区别
    【解决方案3】:

    您的代码看起来不错,您的操作系统/设备一定很慢。

    如果有任何其他代码从您的 onBackPressed() 执行,而您没有在此处发布,那可能是因为其他一切看起来都不错。

    【讨论】:

    • 编辑了我的代码看看。不能是我的手机,因为它有 3 GB 的 RAM,而且我在不同的手机上试过
    【解决方案4】:

    通过在我的应用主题中将 windowIsTranslucent 设置为 false 来解决它。

    【讨论】:

      【解决方案5】:

      首先你应该初始化 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();
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-01-22
        • 1970-01-01
        • 1970-01-01
        • 2022-06-28
        • 1970-01-01
        • 1970-01-01
        • 2011-02-02
        • 2014-11-11
        相关资源
        最近更新 更多