【问题标题】:pressing back button from toolbar minimizes the app when opened from notification从通知打开时,从工具栏按下后退按钮会最小化应用程序
【发布时间】:2017-07-15 02:40:16
【问题描述】:

我正在使用 firebase 云功能进行通知,点击通知后,应用程序会打开一个特定的活动。 如何通过单击工具栏上的后退按钮并单击 Android 后退按钮来转到主活动或上一个活动。 现在应用程序在点击后退按钮时最小化。

我正在使用下面的代码,但是当通过通知打开时应用程序仍然最小化。

   @Override
    public void onBackPressed() {
    Intent i = new Intent(this, FrontActivity.class);
    i.putExtra("exit", true);
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

    startActivity(i);
}

如果应用程序没有从通知中打开,则返回按钮可以正常工作。

【问题讨论】:

    标签: android android-notifications


    【解决方案1】:

    对于向上按钮(工具栏箭头):

     @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
                case android.R.id.home:
                    Intent intent = new Intent(this, MainActivity.class);
                    startActivity(intent);
                default:
                    return super.onOptionsItemSelected(item);
            }
        }
    

    对于 NavigationBar 中的后退按钮,您可以覆盖 onBackPressed() 方法 (see also)。

    【讨论】:

      【解决方案2】:

      简单的方法是添加finish();

      @Override
      public void onBackPressed() {
      finish();
      }
      

      其他方式是

          @Override  
          public boolean onOptionsItemSelected(MenuItem item) {  
          switch (item.getItemId()) {
              case android.R.id.home:
                  finish();
                  return true;
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-28
        • 1970-01-01
        • 2014-01-02
        • 2019-12-07
        • 1970-01-01
        • 2016-08-31
        相关资源
        最近更新 更多