I developed one apps in that one i used view pager using fragment.In that i move to one page to another using fragment class.in second fragment class i have one button back on that i write coding to move direct to first fragment through fragment.replace but after replacing first fragment i don't scroll pager page to one to another means when i click on back button i move to first fragment but i not able to go back to that class againg. So i can not have idea to hove move/scroll page again to go first to second fragment. please help me quickly,thanks in advance.
My First class follewing :
enter code here
public class ViewPagerMainActivity extends FragmentActivity implements
OnClickListener, OnPageChangeListener {
// all variable declare here
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD,
WindowManager.LayoutParams.FLAG_FULLSCREEN
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
);
setTheme(android.R.style.Theme_Light_NoTitleBar);
setContentView(R.layout.view_pager_main);
// other code here
}
private class MyPagerAdapter extends FragmentPagerAdapter {
public MyPagerAdapter(
android.support.v4.app.FragmentManager fragmentManager) {
super(fragmentManager);
}
@Override
public Fragment getItem(int pos) {
switch (pos) {
case 0:
return FirstFragment.newInstance("");
case 1:
return SecondFragment.newInstance("");
default:
return SecondFragment.newInstance("Default");
}
}
@Override
public int getCount() {
return 2; // return no of fragment created by us
}
}
@Override
public void onClick(View v) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
}
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
}
}
enter code here SECOND FILE
public class FirstFragment extends Fragment implements OnTouchListener {
// all variable decalre here
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
getActivity().setTheme(android.R.style.Theme_Light_NoTitleBar);
v = inflater.inflate(R.layout.sliding_lock_pager, container, false);
// other code here
return v;
}
public static FirstFragment newInstance(String text) {
FirstFragment f = new FirstFragment();
Bundle b = new Bundle();
b.putString("msg", text);
f.setArguments(b);
return f;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
return false;
}
} third file enter code here public class SecondFragment extends Fragment implements OnClickListener {
// all varible decalare here
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
getActivity().setTheme(android.R.style.Theme_Light_NoTitleBar);
View v = inflater.inflate(R.layout.password, container, false);
// other code here
// this method move to main pager
txtBack.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final android.support.v4.app.FragmentTransaction ft = getFragmentManager()
.beginTransaction();
ft.replace(R.id.layputMainPager, new FirstFragment(),
"NewFragmentTag");
ft.addToBackStack(null);
ft.commit();
}
});
return v;
}
public static SecondFragment newInstance(String text) {
SecondFragment f = new SecondFragment();
Bundle b = new Bundle();
b.putString("msg", text);
f.setArguments(b);
return f;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
See in secondFragment class when i back through back button then i want to scroll again to that page i not do that. i move again to that page though page scroll. and in ft.replace(R.id.layputMainPager, new FirstFragment()) R.id.layputMainPager is `class ViewPagerMainActivity` class xml files a
main layout id.
There are a better implementation with the interfaces.
should replaced by that: Fragment file
On your acitivity File:
I have gone through same problem few days back, I think you are mixing 2 different implementations:
If you are using fragments with ViewPager. You can use ViewPager's setCurrentItem() method to move to next or prev fragment.
If you use only fragments then to move between fragments you can to use FragmentTransaction class methods like replace(), add() etc.
Hope this will clear things. Please share code if something is still unclear.
EDIT
You can overload onBackPressed() method something like :
If you are added Button in your fragment you can do something like :
Edit : Added Code
FirstFragment File
SecondFragment File
Please see the onBackPressed() Method and back.setOnClickListener() in SecondFragment file. Hope this will help