So I have a View Pager class in which I have four different fragment classes with different layouts. I have buttons on each Fragment and those buttons once clicked should take me to new activities. But the problem is once I run the application the button on the first fragment works but not the other fragments. Then again after I swipe to the other fragments and come back to the first fragment, the button which was working in the first fragment does not work anymore. There is no exception shown in the Logcat so i dont know where the error is. Can anybody please help me on this. Below is the code for my View Pager and Adapter followed by the code of one of my fragments as the codes for the other fragments are just the same with differnet button names
public class Home_Pager extends FragmentActivity {
FragmentAdapter mAdapter;
ViewPager pager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_tablet_pager);
setContentView(R.layout.activity_home_pager);
mAdapter = new FragmentAdapter(getSupportFragmentManager());
pager = (ViewPager)findViewById(R.id.pager);
pager.setAdapter(mAdapter);
}
public class FragmentAdapter extends FragmentStatePagerAdapter {
public FragmentAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment fragment = new Fragment();
switch(position){
case 0:
fragment = new Mobility();
break;
case 1:
fragment = new DesktopAIO();
break;
case 2:
fragment = new Monitor_Projector();
break;
case 3:
fragment = new SmartPhone();
break;
}
return fragment;
}
@Override
public int getCount() {
return 4;
}
}
This is the code for one of my fragment
public class Mobility extends Fragment{
View myFragmentView;
public static final String EXTRA_MESSAGE = "EXTRA_MESSAGE";
public static final Mobility newInstance(String message)
{
Mobility f = new Mobility();
Bundle bdl = new Bundle(1);
bdl.putString(EXTRA_MESSAGE, message);
f.setArguments(bdl);
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myFragmentView = inflater.inflate(R.layout.activity_mobility, container, false);
return myFragmentView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Button x = (Button) getActivity().findViewById(R.id.button1);
x.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent firstpage= new Intent(getActivity(),Tablet_Home.class);
getActivity().startActivity(firstpage);
}
});
}
Please guys give me a solution. Thank You