【问题标题】:Application is crashing when BackPressed on screenBackPressed 在屏幕上时应用程序崩溃
【发布时间】:2015-05-21 04:11:12
【问题描述】:

当用户在我的应用程序的登录屏幕上按回时应用程序崩溃。我使用下面的代码来启动我的登录活动 -

private void showLoginActivity() {
    Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
    finish();
}

我通过异步任务在这个屏幕上做一些后台操作。我看到 logcat 在登录活动完成并且任务仍在运行时,它在 asynctask 中显示问题。我该如何解决这个问题?

提前致谢

【问题讨论】:

  • 发布您的 logcat 条目。
  • 你能显示错误信息吗....logcat 信息
  • 这可能是因为您从异步任务中引用了第一个活动中的对象。您可以通过在任务完成后完成 tha 活动来避免这种情况。
  • 为什么你在 Activity 中使用 getApplicationContext()(finish() 调用后的假设)

标签: android android-layout android-asynctask


【解决方案1】:

如果请尝试一下:

Intent i = new Intent(getApplicationContext(), LoginActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i);

【讨论】:

    【解决方案2】:

    找到解决方案。我使用了一些标志来启动登录活动。以下是我使用的那些标志 -

    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
    

    【讨论】:

    • 当然这不是一个解决方案 ...这是相当肮脏的修复...与我们分享 logcat 的日志 - 应该有一个真正的解决方案 ....
    【解决方案3】:

    我认为你的标志有问题。可能在你的

    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    我建议您 this 阅读和理解。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-07
      相关资源
      最近更新 更多