0

I have Main_Activity inside of it a ViewPager2 which contains 3 Fragment. So I'm trying to use onResume() on the MainActivity to check if the user is connected to the internet. If he's connected i will display an Ad view. but the onResume() called only the first time, and when the user change the fragment onResume() doesn't called. I think the reason why is because of the MainActivity lost its foucs. So is there anyway.

Main_Activity.xml

<com.google.android.material.tabs.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toTopOf="@id/guideline_v2"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:background="@color/black_action_bar"
        app:tabTextColor="@color/gris"
        app:tabGravity="fill"
        app:tabMode="fixed"
        app:tabMaxWidth="0dp"
        app:tabSelectedTextColor="@color/green_org"
        app:tabIndicatorColor="@color/green_org"
        app:tabTextAppearance="@style/customfontstyle"
        >
        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="IMAGES" />
        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="VIDEOS" />
        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="SAVED" />




    </com.google.android.material.tabs.TabLayout>



    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@id/guideline_v2"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_height="0dp"/>

MainActivity.java

FragmentManager fragmentManager = getSupportFragmentManager();
    pagerAdapter = new PagerAdapter(fragmentManager,getLifecycle());
    pagerAdapter.addFragment(new ImagesFragment());
    pagerAdapter.addFragment(new VideosFragment());
    pagerAdapter.addFragment(new SavedFragment());
    viewPager.setAdapter(pagerAdapter);
    

    tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });

    viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
        @Override
        public void onPageSelected(int position) {
            super.onPageSelected(position);
            tabLayout.selectTab(tabLayout.getTabAt(position));
        }
    });
S4D3V
  • 13
  • 5
  • Why do You think that `onResume()` should be called when You change a page in Your `ViewPager`? Is that really what You mean in that question? – sweak Feb 12 '22 at 22:06
  • the reason i want to use that to set the ad view GONE when there's no internet. and that view is on the `MainActivity` – S4D3V Feb 12 '22 at 22:14

1 Answers1

0

The Fragment and Activity have different onResume methods.

So with the default Fragment lifecycle when you swipe left and right the onResume of ImagesFragment, VideosFragment and SavedFragment get called not the onResume method of MainActivity

Update based on comment

It sounds like you are doing the Internet check in the wrong place, it should probably not be in the MainActivity onResume method as there is no guarantee that this is called when you move out of cell range or turn on or off mobile data or wifi.

It sounds like the Internet connectivity check should be in a Broadcaster Receiver that is listens for network connectivity state changes. Quick scan of StackOverflow shows this as how to do that https://stackoverflow.com/a/26114247/2373819

Andrew
  • 8,198
  • 2
  • 15
  • 35
  • the reason i want to use that to set the ad view `GONE` when there's no internet. and that view is on the `MainActivity`. So i want to return the focus to the `MainActivity` – S4D3V Feb 12 '22 at 22:13
  • `resumed` is not `focused`, `MainActivity` will be in the `resumed` state already when the it is showing one of the Fragments, therefore it cannot moved to `resumed` state and `onResume` called. Activity and Fragments have different lifecycles. – Andrew Feb 12 '22 at 22:45
  • so is there anyway to change viewpager size when the internet available. – S4D3V Feb 12 '22 at 22:48