androidandroid-fragmentsandroid-viewpagerfragmentpageradapter

how to fix" 3 fragment in One activity with viewPager need validation first two page then possible to go third page"


I want to create a createOrderActivity where has three fragments like Service info, ScheduleInfo, confirmation

the service info fragment has editText if click NextButton(which is situated CreateOrderActivity) check validation editText first. then move ScheduleFragment page. if first two pages validation ok then move to Confirmation Fragment page.

Here is the FragmentViewpagerAdapter class

public class FragmentViewPagerAdapter extends FragmentPagerAdapter {

private final List<Fragment> fragmentList = new ArrayList<>();
private final List<String> fragmentTitleList = new ArrayList<>();

public FragmentViewPagerAdapter(FragmentManager manager) {
    super(manager);
}

@Override
public Fragment getItem(int position) {
    return fragmentList.get(position);
}

@Override
public int getCount() {
    return fragmentList.size();
}

public void addFragment(Fragment fragment, String title) {
    fragmentList.add(fragment);
    fragmentTitleList.add(title);
}

@Override
public CharSequence getPageTitle(int position) {
    return fragmentTitleList.get(position);
}

}

In CreateOrderActivity class

        @Override
        public void onPageSelected(int position) {

            boolean checkSch= false;
            if (position == 1) {

                ServiceInfoFragment serviceInfoFragment = new ServiceInfoFragment();

                //checking validation from ServiceInfoFragment fragment Class
                if (serviceInfoFragment.checkServiceValidation()) {


                    checkSch = true;
                    //Toast.makeText(CreateOrderActivity.this, "Validation okay", Toast.LENGTH_SHORT).show();
                }else {
                    checkSch = false;
                   // Toast.makeText(CreateOrderActivity.this, "Please check validation", Toast.LENGTH_SHORT).show();
                    pagerCreateOrder.setCurrentItem(position-1);
                }
            }
            if (position == 2) {

                if (checkSch){
                    ScheduleFragment scheduleFragment = new ScheduleFragment();
                    if (scheduleFragment.checkScheduleValidation()) {
                        Toast.makeText(CreateOrderActivity.this, "Validation okay", Toast.LENGTH_SHORT).show();
                    }else {
                        Toast.makeText(CreateOrderActivity.this, "Please check S validation", Toast.LENGTH_SHORT).show();
                        pagerCreateOrder.setCurrentItem(position-1);
                    }

                }else {

                    Toast.makeText(CreateOrderActivity.this, "Please check validation", Toast.LENGTH_SHORT).show();
                    pagerCreateOrder.setCurrentItem(position-2);
                }


            }


        }

//checking checkScheduleValidation() in ScheduleFragment class. return null exception

*bellow mehtod declear in Fragment *

    public boolean checkServiceValidation(){


    return true;
}

I upload this Image

I did like this image

I used this Reference


Solution

  • I found a solution in my way. I have loaded three fragments in a viewpager. In the second fragment, there is one edittext. on clicking the next button, there is a validation for checking email. On the basis of validation next fragment is loaded. All the Fragments are loaded as singleton . You might have caused null pointer exception , because of multiple instance of fragments.

    public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private SectionsPagerAdapter mSectionsPagerAdapter;
    private FragmentTwo fragmentTwo;
    
    private ViewPager mViewPager;
    Button back, next;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        back = findViewById(R.id.back);
        next = findViewById(R.id.next);
        next.setOnClickListener(this);
        back.setOnClickListener(this);
    
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
    
        mViewPager = (ViewPager) findViewById(R.id.vp_viewpager);
        mViewPager.setAdapter(mSectionsPagerAdapter);
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {
    
            }
    
            @Override
            public void onPageSelected(int i) {
                if (i==2){
                    if (!fragmentTwo.checkEditText()) {
                        Toast.makeText(getApplicationContext(),"False",Toast.LENGTH_LONG).show();
                        mViewPager.setCurrentItem(i-1);
                        return;
                    }
                }
    
    
            }
    
            @Override
            public void onPageScrollStateChanged(int i) {
    
            }
        });
    
    
    }
    
    private void changeViewPagerPosition(int position) {
        int totalCount = mViewPager.getAdapter().getCount();
        if (position < 0 || position >= totalCount) {
            return;
        }
        mViewPager.setCurrentItem(position);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
    
    @Override
    public void onClick(View view) {
        int currentViewpagerPosition = mViewPager.getCurrentItem();
    
        switch (view.getId()) {
            case R.id.back:
                changeViewPagerPosition(currentViewpagerPosition - 1);
                break;
            case R.id.next:
                if (currentViewpagerPosition==1){
                    if (!fragmentTwo.checkEditText()) {
                        Toast.makeText(getApplicationContext(),"Falsee",Toast.LENGTH_LONG).show();
                        return;
                    }
    
                }
                changeViewPagerPosition(currentViewpagerPosition + 1);
                break;
        }
    }
    
    public class SectionsPagerAdapter extends FragmentPagerAdapter {
    
        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }
    
        @Override
        public Fragment getItem(int position) {
            switch (position) {
    
                case 0:
                    return FragmentOne.newInstance(position);
                case 1:
                    return fragmentTwo=FragmentTwo.getInstance();
                case 2:
                    return FragmentThree.newInstance(position);
                default:
                    return FragmentThree.newInstance(position);
    
            }
    
        }
    
        @Override
        public int getCount() {
            return 3;
        }
    }
    

    }

    The Fragment 2

    public  class FragmentTwo extends Fragment  {
    
    EditText email;
    private static FragmentTwo fragment=null;
    
    
    public FragmentTwo() {
    }
    
    public static FragmentTwo getInstance() {
        if (fragment == null){
            fragment = new FragmentTwo();
        }
        return fragment;
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_two, container, false);
       return rootView;
    }
    
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    
    
        email = getView().findViewById(R.id.editText);
    
    }
    
    
    public boolean emailValidator()
    {
        Pattern pattern;
        Matcher matcher;
        final String EMAIL_PATTERN = "^[_A-Za-z0-" +
                "9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
        pattern = Pattern.compile(EMAIL_PATTERN);
        matcher = pattern.matcher(email.getText().toString());
        return matcher.matches();
    }
    
    
    public Boolean checkEditText(){
        if (emailValidator()){
            return true;
        }
        return false;
    }
    

    }