0

I'm trying to create Login by using ViewPager.

The problem is when I press Login in FragmentAccount it goes to FragmentPage, but the information in FragmentPage is not updated.

(My theory) To me it feels like when FragmentAccount is loaded it also Preloads FragmentPage, therefore when it loads up the page it used non-update information for FragmentPage. Though, I don't know enough about Android Studio...

I think the problem lies within the first two classes, but I added other two just in case.

I very much appreicate the help.

I did test, and the data is stored correctly.

User Enters log in details and presses Log in button:

public class FragmentAccount extends Fragment implements View.OnClickListener {

    private static final String TAG = "FragmentAccount";

    private EditText fieldUsername;
    private EditText fieldPassword;
    private Button bLogIn;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.fragment_account, container, false);

        fieldUsername = (EditText) view.findViewById(R.id.fragment_account_field_username);
        fieldPassword = (EditText) view.findViewById(R.id.fragment_account_field_password);

        bLogIn = (Button) view.findViewById(R.id.fragment_account_button_login);
        bLogIn.setOnClickListener(this);
        return view;
    }

    @Override
    public void onClick(View view) {
        boolean logedIn = MainActivity.user.logIn(fieldUsername.getText().toString(), fieldPassword.getText().toString());
        fieldPassword.setText("");
        if (logedIn) {
            fieldUsername.setText("");
            ((MainActivity) getActivity()).setViewPager(5);
        }
    }
}

The log in details should be displayed here:

public class FragmentProfile extends Fragment {

    private static final String TAG = "FragmentProfile";

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_profile, container, false);

        final TextView usernameText = (TextView) view.findViewById(R.id.fragment_profile_text_username_2);
        final TextView passwordText = (TextView) view.findViewById(R.id.fragment_profile_text_password_2);
        final Button bLogOut = (Button) view.findViewById(R.id.fragment_profile_button_logout);

        String s1 = MainActivity.user.getLogedInUsername();
        String s2 = MainActivity.user.getLogedInPassword();

        usernameText.setText(s1);
        passwordText.setText(s2);

        Log.i(TAG, s1 + ", " + s2);

        bLogOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MainActivity.user.logOut();
                ((MainActivity) getActivity()).setViewPager(4);
            }
        });

        return view;
    }
}

Where the log in data is stored:

public class Users {

    private static final String TAG = "Users";

    private boolean isUserLogedIn = false;
    private String logedInUsername = "";
    private String loggedInPassword = "";

    final private String adminUsername = "1";
    final private String adminPassword = "2";

    public Users(){

    }

    public boolean logIn(String usr, String pass){
        if( usr.equals(adminUsername) && pass.equals(adminPassword)){
            isUserLogedIn = true;
            logedInUsername = usr;
            loggedInPassword = pass;

            Log.i(TAG, logedInUsername + ", " + loggedInPassword);
            return isUserLogedIn;
        }else{
            return isUserLogedIn;
        }
    }

    public void logOut(){
        isUserLogedIn = false;
        logedInUsername = "";
        loggedInPassword = "";
    }

    public boolean isUserLogedIn(){
        return isUserLogedIn;
    }

    public String getLogedInUsername(){
        return logedInUsername;
    }

    public String getLogedInPassword(){
        return loggedInPassword;
    }
}

The main activity, and where I change between the pages

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{

    static final public Users user = new Users();
    private SectionsStatePageAdapter mSectionsStagePageAdapter;
    private ViewPager mViewPager;

    private DrawerLayout mDrawerLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //body
        mSectionsStagePageAdapter = new SectionsStatePageAdapter(getSupportFragmentManager());
        mViewPager = (ViewPager) findViewById(R.id.container);

        setupViewPager(mViewPager);

        //sidebar
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
        setNavigationListner();
    }

    private void setNavigationListner(){
        NavigationView mNavigationview = (NavigationView) findViewById(R.id.nav_view);
        mNavigationview.setNavigationItemSelectedListener(this);
    }

    /**
     * Add all of the fragment to the adapter, so we can change the fragment that is displayed.
     *
     * @param viewPager
     */
    private void setupViewPager(ViewPager viewPager){
        SectionsStatePageAdapter adapter = new SectionsStatePageAdapter(getSupportFragmentManager());
        adapter.addFragment(new FragmentDictionary(), "FragmentDictionary"); //0
        adapter.addFragment(new FragmentCheck(), "FragmentCheck"); //1
        adapter.addFragment(new FragmentHome(), "FragmentHome");//2
        adapter.addFragment(new FragmentPricetag(), "FragmentPricetag");//3
        adapter.addFragment(new FragmentAccount(), "FragmentAccount");//4
        adapter.addFragment(new FragmentProfile(), "FragmentProfile"); //5
        adapter.addFragment(new FragmentHelp(), "FragmentHelp");//7
        adapter.addFragment(new FragmentSettings(), "FragmentSettings");//8
        adapter.addFragment(new FragmentAboutUs(), "FragmentAboutUs");//9
        viewPager.setAdapter(adapter);
        setViewPager(2);
    }

    /**
     * Changes the fragment by passing the corresponding i to the fragment in the adapter
     * @param fragmentNumber
     */
    public void setViewPager(int fragmentNumber){
        mViewPager.setCurrentItem(fragmentNumber);
    }

    public void setmDrawerLayoutVisable(boolean setVisible){
        if(setVisible){
            mDrawerLayout.openDrawer(Gravity.RIGHT);
        }else{
            mDrawerLayout.closeDrawer(Gravity.RIGHT);
        }
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {

        switch(item.getItemId()){
            case R.id.nav_account:
                if(user.isUserLogedIn()){
                    setViewPager(5);
                }else{
                    setViewPager(4);
                }
                setmDrawerLayoutVisable(false);
                break;
            case R.id.nav_help:
                setViewPager(7);
                setmDrawerLayoutVisable(false);
                break;
            case R.id.nav_settings:
                setViewPager(8);
                setmDrawerLayoutVisable(false);
                break;
            case R.id.nav_about_us:
                setViewPager(9);
                setmDrawerLayoutVisable(false);
                break;
            default:
                break;
        }

        return false;
    }
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • change your `SectionsStatePageAdapter ` from `FragmentPagerAdapter` to `FragmentStatePagerAdapter` – akhilesh0707 Oct 13 '17 at 13:39
  • try this ref link https://stackoverflow.com/questions/7263291/viewpager-pageradapter-not-updating-the-view – akhilesh0707 Oct 13 '17 at 13:40
  • `I'm trying to create Login by using ViewPager.` Why? – Eselfar Oct 13 '17 at 13:55
  • @akhilesh0707 Thank you for the link, I will definitely use the link for reference and see if I can make my code better. Though, even after skimming the reference link, I don't understand what to change from what. – Lukas Povilonis Oct 13 '17 at 13:58
  • @Eselfar What another way would you recommend? I'm aware that I can do it with intents and changing activities. Though, I'm also using a fragment instead of activity bar and NavigationView (for side-bar), if I was to create a new intent, then I would have to copy these. – Lukas Povilonis Oct 13 '17 at 14:02
  • You can use ONE Activity with MULTIPLE Fragments without using a ViewPager. The Activity stores your information, you implement the communication between the Activity and the Fragments using the [official documentation](https://developer.android.com/training/basics/fragments/communicating.html), and you replace the existing Fragment by the next one when the user presses next. By adding the transaction in the BackStack you allow the user to go back to the previous step. ViewPager are not design to so that and are always a pain in the a**e anyway. – Eselfar Oct 13 '17 at 14:06
  • @Eselfar I see. So ViewPager is best avoided when possible. Just to clarify, are ViewPagers would be used for stand-alone pages (where going back is not needed)? – Lukas Povilonis Oct 13 '17 at 14:14
  • Generally you use ViewPagers together with a TabLayout. In that case you have a fixed number of Views that the user can swich from in any order. An other case is when you want to display a list of pages (like a tutorial, process, catalog,..). But I'd not recommend it to do a steps form as it's a big mess to manage and there is no gain compared to the solution described above. Plus in this last case you'll probably have to deactivate the swipe gesture, which is not so easy. – Eselfar Oct 13 '17 at 14:17

1 Answers1

1

Your theory is indeed correct.

By default the ViewPager loads 2 extra pages, the one before and the one after. Unfortunately this is also the minimum offscreen page limit.

You will have to implement a way for those fragments to communicate with each other, either EventBus or some other method.