【问题标题】:How to clear an activity from the second activity going back to home screen in android?如何从第二个活动中清除一个活动返回到android中的主屏幕?
【发布时间】:2013-06-26 21:34:40
【问题描述】:

示例场景是: 从登录屏幕-主屏幕-然后当我单击隐藏按钮时,应用程序将转到主屏幕,当我再次单击应用程序时,将调用主屏幕

【问题讨论】:

    标签: java android


    【解决方案1】:

    当你想显示主屏幕时触发一个意图

    Intent setIntent = new Intent(Intent.ACTION_MAIN);
    setIntent.addCategory(Intent.CATEGORY_HOME);
    setIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(setIntent); 
    

    所以这将在按下隐藏按钮时触发

    【讨论】:

      【解决方案2】:

      我认为你可以使用你可以使用 FLAG_ACTIVITY_CLEAR_TOP

      FirstActivity is the first activity in the application:
      
      public static void home(Context ctx) {
          if (!(ctx instanceof FirstActivity)) {
              Intent intent = new Intent(ctx, FirstActivity.class);
              intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
              ctx.startActivity(intent);
          }
      }
      
      And If you want to exit from the whole application,this help you in that.
      
       public static void clearAndExit(Context ctx) {
          if (!(ctx instanceof FirstActivity)) {
              Intent intent = new Intent(ctx, FirstActivity.class);
              intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
              Bundle bundle = new Bundle();
              bundle.putBoolean("exit", true);
              intent.putExtras(bundle);
              ctx.startActivity(intent);
          } else {
              ((Activity) ctx).finish();
          }
      }
      
      i Really Hope this helps.
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-02
        • 1970-01-01
        • 2011-10-24
        • 1970-01-01
        • 2012-07-02
        相关资源
        最近更新 更多