Xamarin.forms: Android/Forms Tabpage OnAppearing being called twice

Created on 4 Apr 2018  路  13Comments  路  Source: xamarin/Xamarin.Forms

https://bugzilla.xamarin.com/show_bug.cgi?id=51574

Correct me if I am wrong but pretty certain this issue below is similar but not the same as it is regarding the OnDisappearing (see below):
https://github.com/xamarin/Xamarin.Forms/issues/1438

Description

The OnAppearing method is being called twice for the child tab contentpage that is being displayed when TabbedPage is set as main page of application.

Steps to Reproduce

  1. Run app with TabbedPage set as mainpage which has Tab1Page and Tab2Page as its children.
  2. Tab1Page OnAppearing method being called twice
  3. Tab2Page OnAppearing method called once

Expected Behavior

Tab1Page Constructor called.
Tab2Page Constructor called.
Tab1Page OnAppearing method called first time.
Tab2Page OnAppearing method called first time.

Actual Behavior

Tab1Page Constructor called.
Tab2Page Constructor called.
Tab1Page OnAppearing method called first time.
Tab1Page OnDisappearing method called first time.
Tab1Page OnAppearing method called second time.
Tab2Page OnAppearing method called first time.

Basic Information

  • Version with issue: 2.5.0.280555
5 help wanted inactive Android bug up-for-grabs

Most helpful comment

Hello,
I am facing a similar issue with TabbedPage OnAppearing calls and thought I would add my finding to this issue.

We have TabbedPage and each child of TabbedPage is a NavigationPage which is causing different behaviour in Android. If i remove the NavigationPage wrapper then it is working as expected.

Layout is like this
TabbedPage
-> NavigationPage -> ContentPage
-> NavigationPage -> ContentPage

In this Layout which is created dynamically in cs file and added as children to TabbedPage, the following difference is found in terms of lifecycle activity calls

Page Load - Android
Constructor - Page 1
Constructor - Page 2
Constructor - Page 3
TabbedPage OnAppearing
OnAppearing - Page 1
OnAppearing - Page 2 (Extra)
OnAppearing - Page 3 (Extra)
Moving to 2nd Tab
OnDisappearing - Page 1
聽_(Missing call to 2nd Tab onAppearing)_
Moving to 3nd Tab
OnDisappearing - Page 2
聽_(Missing call to 3nd Tab onAppearing)_聽
Moving back to 2nd Tab (Working properly)
OnDisappearing - Page 3
OnAppearing - Page 2
Moving back to 1st Tab (Working properly)
OnDisappearing - Page 2
OnAppearing - Page 1

Please find the attached sample project
TabbedPageTest.zip

All 13 comments

Fully agree @LeoJHarris, I have TabbedPage with 4 tabs, and all of them call OnAppearing method at the same time the app start, it's make the app slower at starup.

@xamarindevelopervietnam they get called when Android decides to load the renderers for the tabs. Android keeps a certain amount of pages in cache in order to make side scrolling fast. There probably should be a way to configure this behavior.

I am unable to reproduce this issue on 2.5.1 stable. Can you please confirm it still happens on there and if so produce a small reproduction case?

Issue still happening see attached repo.

OnAppearing method is being called twice within the first inserted tabbed page. See the output and page content labels that also output the number of times methods being called. Doesn't seem right to me.

Sample.zip

Hello,
I am facing a similar issue with TabbedPage OnAppearing calls and thought I would add my finding to this issue.

We have TabbedPage and each child of TabbedPage is a NavigationPage which is causing different behaviour in Android. If i remove the NavigationPage wrapper then it is working as expected.

Layout is like this
TabbedPage
-> NavigationPage -> ContentPage
-> NavigationPage -> ContentPage

In this Layout which is created dynamically in cs file and added as children to TabbedPage, the following difference is found in terms of lifecycle activity calls

Page Load - Android
Constructor - Page 1
Constructor - Page 2
Constructor - Page 3
TabbedPage OnAppearing
OnAppearing - Page 1
OnAppearing - Page 2 (Extra)
OnAppearing - Page 3 (Extra)
Moving to 2nd Tab
OnDisappearing - Page 1
聽_(Missing call to 2nd Tab onAppearing)_
Moving to 3nd Tab
OnDisappearing - Page 2
聽_(Missing call to 3nd Tab onAppearing)_聽
Moving back to 2nd Tab (Working properly)
OnDisappearing - Page 3
OnAppearing - Page 2
Moving back to 1st Tab (Working properly)
OnDisappearing - Page 2
OnAppearing - Page 1

Please find the attached sample project
TabbedPageTest.zip

@sriram15 and @xamarindevelopervietnam:

I'm afraid your experience is unrelated to the initial discussion. As @jassmith mentioned, this is expected behavior on Android. The ViewPager preloads a minimum of 1 page on either side of the selected tab. The default is 3 pages. This is configurable via platform specific:

using Xamarin.Forms.PlatformConfiguration;
using Xamarin.Forms.PlatformConfiguration.AndroidSpecific;

namespace TabbedPageTest
{
    public partial class TabbedPageTestPage : Xamarin.Forms.TabbedPage
    {
        public TabbedPageTestPage()
        {
            InitializeComponent();

        On<Android>().SetOffscreenPageLimit(1);

            Children.Add(new NavigationPage(new TabPage1()));
            Children.Add(new NavigationPage(new TabPage2()));
            Children.Add(new NavigationPage(new TabPage3()));
        }
...

If you attempt to set the OffscreenPageLimit to 0 (i.e., do not preload any pages), Android will intervene:

W/ViewPager(30495): Requested offscreen page limit 0 too small; defaulting to 1

We can't prevent it from preloading the renderers for at least one tab on either side of the selected tab. Android does this for user experience, and it apparently will not let us make that decision for it. However, we can discuss what you expect from the event lifecycle. We have discussed this before on https://forums.xamarin.com/discussion/84510/improved-life-cycle-support and other threads, and I think it's worth adding to that discussion that the OS may force those events on us in somewhat unexpected ways.

TLDR; We fully expect there to be Appearing events called for Tabs that are not necessarily visible on the screen. We do not expect the events to be called more than once for a single tab, as @LeoJHarris describes.

Hello,

Not sure it is directly related to this issue (although it looks pretty much the same), I've encountered a regression in my app.

Using the attached samples, please find below the behaviour, which is not the same depending on the version of XF. Nothing else is changed in the source code (apart the namespaces from BottomBarOK to BottomBarKO).

BottomBarOK - Forms 2.5.0.280555

OnAppearing ItemsPage       <= initial launch

OnDisappearing ItemsPage    <= clicked on About tab
OnAppearing AboutPage

OnDisappearing AboutPage    <= clicked on Items tab
OnAppearing ItemsPage

OnDisappearing ItemsPage    <= clicked on About tab
OnAppearing AboutPage

BottomBarKO - Forms 3.0.0.561731

OnAppearing ItemsPage       <= initial launch

OnDisappearing ItemsPage    <= clicked on About tab (first time ok!!)
OnAppearing AboutPage

OnDisappearing AboutPage    <= clicked on Items tab
OnAppearing ItemsPage       <= Wrong!
OnDisappearing ItemsPage    <= Wrong!
OnAppearing ItemsPage

OnDisappearing ItemsPage    <= clicked on About tab
OnAppearing AboutPage       <= Wrong!
OnDisappearing AboutPage    <= Wrong!
OnAppearing AboutPage

Any explanation?
Am I doing something wrong?
BottomBarKO.zip
BottomBarOK.zip

Just to mention that XF 2.5.0.280555 is the last known working version.
The first version to expose this weird behaviour is XF 2.5.1.444934.

Anyone? Should I create a new issue or is it in the right place?

Update: it now seems to work as intended, i.e. without me changing anything but the nuget package, with latest XF 3.1.0.583944!

I still have the issue even with XF 3.1.0.583944. Not on Tab page though, it is a normal content page.

tabbed pages appears fine now regarding the onappearing, cant comment on non tab pages though.

Consolidating OnAppearing issues here
https://github.com/xamarin/Xamarin.Forms/issues/6919

Was this page helpful?
0 / 5 - 0 ratings