I have a use case in which I use a ViewPager with TabLayout. I have used the Android tutorial linked here.
My Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.viewpager.widget.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/notification_view_pager">
<com.google.android.material.tabs.TabLayout
android:id="@+id/notification_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</androidx.viewpager.widget.ViewPager>
</LinearLayout>
My activity
public class MyActivity extends AppCompatActivity
{
int currentFocussedTab = -1;
private TabLayout notificationsTabLayout;
private ViewPager notificationsViewPager;
private FragmentStatePagerAdapter notificationsStatePagerAdapter;
private Fragment fragmentOne;
private Fragment fragmentTwo;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_activity);
// Setting up tabbed layouts.
notificationsTabLayout = findViewById(R.id.notification_tabs);
notificationsTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab)
{
int position = tab.getPosition();
currentFocussedTab = position;
// Custom function to log click.
if (position == 0) {
fragmentOne.logClick();
} else {
fragmentTwo.logClick();
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab)
{
int position = tab.getPosition();
if (position == 0) {
fragmentOne.logUnselect();
} else
fragmentTwo.logUnselect();
}
}
@Override
public void onTabReselected(TabLayout.Tab tab) {}
});
// Set up view pager.
notificationsViewPager = findViewById(R.id.notification_view_pager);
notificationsViewPager.setCurrentItem(0);
notificationsTabLayout.setupWithViewPager(notificationsViewPager);
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentOne = new FragmentOne();
fragmentTwo = new FragmentTwo();
notificationsStatePagerAdapter = new FragmentStatePagerAdapter(fragmentManager,
FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
@NonNull
@Override
public Fragment getItem(int position)
{
return position == 0 ? fragmentOne : fragmentTwo;
}
@Override
public int getCount()
{
// Two fixed tabs.
return 2;
}
@Nullable
@Override
public CharSequence getPageTitle(int position)
{
return position == 0 ? getText(R.string.act_now_tab) : getText(R.string.others_tab);
}
};
notificationsViewPager.setAdapter(notificationsStatePagerAdapter);
}
}
Both FragmentOne and Fragment two are similarly set up. So I am using FragmentOne as an example
class FragmentOne extends Fragment {
. . . . Define all UI elements . . . .
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
final View view = inflater.inflate(R.layout.fragment_layout, container, false);
... Initialize all the layout UI elements . . .
return view;
}
}
UPDATE
I have a button defined in the fragment layout. The button is initialised in onCreateView
method. When the button is clicked, then I want to launch a new activity.
ISSUE
I have to load another activity from inside the fragment. This is how I am doing it
Intent intent = new Intent(getActivity(), MyNewActivity.class);
getActivity().startActivity(intent);
However, the application terminates because the getActivity()
always returns null. If I don't have the fragment in the view pager, then the function does not return null.
DESIRED BEHAVIOUR
I want to load the new application activity from within a fragment that has been loaded inside the view pager. How do I achieve that?