【问题标题】:android application closing programmaticallyandroid应用程序以编程方式关闭
【发布时间】:2014-03-27 05:50:38
【问题描述】:

我正在开发一个 Android 项目,要求是任何活动中都发生异常。然后应用程序应该在执行 catch 块后自动退出。我知道 Android 应用程序架构不建议自动关闭应用程序。

【问题讨论】:

  • 尝试在 catch 块中的每个活动中调用 finish() 或在您要关闭应用程序的任何位置...
  • 您需要完全关闭应用程序,这意味着后台本身或者您希望在发生异常时还活着。

标签: android exit


【解决方案1】:

使用它来编写应用程序

Intent homeIntent = new Intent(Intent.ACTION_MAIN);
homeIntent.addCategory( Intent.CATEGORY_HOME );
homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(homeIntent);

查看以下链接了解更多详情Android App closing 希望对你有帮助,谢谢:)

【讨论】:

    【解决方案2】:
     Intent intObj=new Intent(this, Home.class);
     intObj.putExtra("finish", true);
     intObj.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | 
                  Intent.FLAG_ACTIVITY_CLEAR_TASK |
                   Intent.FLAG_ACTIVITY_NEW_TASK);
     startActivity(intObj);
    

    【讨论】:

      【解决方案3】:

      要以编程方式(并且优雅地)终止您的 Android 应用程序,请考虑实施完成链机制。具体来说,

      (1) 实现所有 Activity 子类的 onResume(),如下所示。

      protected void onResume()
      {
          super.onResume();
      
          // Check if the application is in the process of
          // stopping. How to implement 'App' in the code
          // below is up to you.
          if (App.getInstance().isStopping())
          {
              finish();
              return;
          }
      
          ......
      

      (2) 当你想终止你的应用程序时,调用如下方法。

      protected void exit()
      {
          // Turn into 'stopping' state.
          App.getInstance().setStopping(true);
      
          // Finish this Activity. This removes this Activity
          // instance from the Activity stack and the next
          // Activity instance will be displayed. And if the
          // next Activity's onResume() is implemented like the
          // above example, the next Activity will finish(), too.
          // This finish-chain mechanism continues until it
          // reaches the root Activity.
          finish();
      }
      

      由于 Android 可能会重用根 Activity 实例而不是“新建”它,因此根 Activity 的 onResume() 必须清除“停止”标志。

      protected final void onResume()
      {
          super.onResume();
      
          if (App.getInstance().isStopping())
          {
              // Close this Activity, meaning that this application is closed
              // because this Activity is the root.
              finish();
      
              // Reset the termination state because Android may reuse this
              // Activity instance instead of creating a new one.
              App.getInstance().setStopping(false);
          }
          else
          {
              ......
          }
      }
      

      实现“退出”(甚至“重启”)机制的示例应用程序:
      https://github.com/TakahikoKawasaki/nv-android-base-sample

      基本根活动示例:
      https://github.com/TakahikoKawasaki/nv-android-base/blob/master/src/main/java/com/neovisionaries/android/app/BaseRootActivity.java

      【讨论】:

        【解决方案4】:

        如果您有多个活动,则可以使用 finishAffinity 关闭所有活动

         finishAffinity();
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-06-29
          • 1970-01-01
          • 1970-01-01
          • 2017-03-31
          • 2014-11-28
          • 2016-04-25
          • 2014-05-21
          相关资源
          最近更新 更多