Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Android/Forms Tabpage OnAppearing being called twice #2240

Closed
LeoJHarris opened this issue Apr 4, 2018 · 13 comments
Closed

Android/Forms Tabpage OnAppearing being called twice #2240

LeoJHarris opened this issue Apr 4, 2018 · 13 comments
Labels
e/5 🕔 5 help wanted We welcome community contributions to any issue, but these might be a good place to start! inactive Issue is older than 6 months and needs to be retested p/Android t/bug 🐛 up-for-grabs We welcome community contributions to any issue, but these might be a good place to start!

Comments

@LeoJHarris
Copy link

LeoJHarris commented Apr 4, 2018

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):
#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
@KennyDizi
Copy link

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.

@jassmith
Copy link

jassmith commented Apr 7, 2018

@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.

@jassmith
Copy link

jassmith commented Apr 7, 2018

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?

@jassmith jassmith added t/bug 🐛 p/Android s/needs-repro ❔ This reported issue doesn't include a sample project reproducing the issue. Please provide one. s/needs-info ❓ A question has been asked that requires an answer before work can continue on this issue. labels Apr 7, 2018
@LeoJHarris
Copy link
Author

LeoJHarris commented Apr 9, 2018

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

@sriram15
Copy link

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

@samhouts samhouts removed s/needs-info ❓ A question has been asked that requires an answer before work can continue on this issue. s/needs-repro ❔ This reported issue doesn't include a sample project reproducing the issue. Please provide one. labels Apr 16, 2018
@samhouts
Copy link
Member

@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.

@nbsoftware
Copy link

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

@nbsoftware
Copy link

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.

@nbsoftware
Copy link

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

@nbsoftware
Copy link

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!

@teejay-87
Copy link

teejay-87 commented Jun 25, 2018

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

@LeoJHarris
Copy link
Author

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

@samhouts samhouts added the e/5 🕔 5 label Jul 23, 2018
@samhouts samhouts added the inactive Issue is older than 6 months and needs to be retested label Dec 26, 2018
@samhouts samhouts added help-wanted up-for-grabs We welcome community contributions to any issue, but these might be a good place to start! and removed help-wanted labels Jun 28, 2019
@samhouts samhouts added the help wanted We welcome community contributions to any issue, but these might be a good place to start! label Jul 2, 2019
@PureWeen
Copy link
Contributor

Consolidating OnAppearing issues here
#6919

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
e/5 🕔 5 help wanted We welcome community contributions to any issue, but these might be a good place to start! inactive Issue is older than 6 months and needs to be retested p/Android t/bug 🐛 up-for-grabs We welcome community contributions to any issue, but these might be a good place to start!
Projects
None yet
Development

No branches or pull requests

8 participants