xamarin/Xamarin.Forms

OnOptionsItemSelected() not called after a configuration/orientation change.

开放

#4,827 创建于 2018年12月20日

 (9 条评论) (0 个反应) (0 位负责人)C# (1,926 个派生)batch import
e/6 :clock6:help wantedinactivep/Androidt/bug :bug:up-for-grabs

仓库指标

星标
 (5,644 个星标)
PR 合并指标
 (30 天内没有已合并 PR)

描述

Description

In my application I have to intercept the software back button click and it works fine as long as I don't change the orientation. After changing the orientation from portait to landscape the method OnOptionsItemSelected() is never called again.

[Activity(Label = "TestApp", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
     protected override void OnCreate(Bundle savedInstanceState)
     {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;
            base.OnCreate(savedInstanceState);

            global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
            LoadApplication(new MyApp());
     }
     protected override void OnPostCreate(Bundle savedInstanceState)
     {
            base.OnPostCreate(savedInstanceState);
            Android.Support.V7.Widget.Toolbar toolbar = this.FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
            SetSupportActionBar(toolbar);
      }

      public override bool OnOptionsItemSelected(IMenuItem item)
      {
            return base.OnOptionsItemSelected(item);
      }
}

styles.xml

<?xml version="1.0" encoding="utf-8" ?>
<resources>

  <style name="MainTheme" parent="MainTheme.Base">
  </style>
  <!-- Base theme applied no matter what API -->
  <style name="MainTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
    <!--If you are using revision 22.1 please use just windowNoTitle. Without android:-->
    <item name="windowNoTitle">true</item>
    <!--We will be using the toolbar so no need to show ActionBar-->
    <item name="windowActionBar">true</item>
    <!-- Set theme colors from http://www.google.com/design/spec/style/color.html#color-color-palette -->
    <!-- colorPrimary is used for the default action bar background -->
    <item name="colorPrimary">#2196F3</item>
    <!-- colorPrimaryDark is used for the status bar -->
    <item name="colorPrimaryDark">#1976D2</item>
    <!-- colorAccent is used as the default value for colorControlActivated
         which is used to tint widgets -->
    <item name="colorAccent">#FF4081</item>
    <!-- You can also set colorControlNormal, colorControlActivated
         colorControlHighlight and colorSwitchThumbNormal. -->
    <item name="windowActionModeOverlay">true</item>

    <item name="android:datePickerDialogTheme">@style/AppCompatDialogStyle</item>
  </style>

  <style name="AppCompatDialogStyle" parent="Theme.AppCompat.Light.Dialog">
    <item name="colorAccent">#FF4081</item>
  </style>
</resources>

toolbar.axml

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

Steps to Reproduce

  1. Start the app
  2. Change orientation
  3. OnOptionsItemSelected() not called an backbutton

Expected Behavior

Actual Behavior

Basic Information

Microsoft Visual Studio Enterprise 2017 Version 15.9.4

Xamarin 4.12.3.77 Xamarin.Android SDK 9.1.4.2

  • Version with issue:
  • IDE:
  • Platform Target Frameworks:
    • Android: 7.0
  • Nuget Packages: Xamarin.Forms 3.4.0.1008975 Xamarin.Android.Support.... 28.0.0
  • Affected Devices: Samsung Galaxy S5

Screenshots

Reproduction Link

贡献者指南