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));
}
});