-2

I have a Login page, so for that I am using SharedPreferences. I am trying to save values. Without values, it shouldn't go to the another Activity for that I have given this in Login page:

 if((spf != null) && (spv !=null) && (et1v != null) && (et2v != null) && (et3v != null) &&

             (sp != null) && (et1 != null) && (et2 != null) && (et3 != null)){

            finish();
            Intent i = new Intent(Login.this,Welcome.class);
            startActivity(i);
     }
       else{
           btn1.setOnClickListener(
                   new View.OnClickListener() {
                       @Override
                       public void onClick(View view) {

                           if (sp.getSelectedItem().toString().length() > 0 &&
                                   et1.getText().toString().length() > 0 &&
                                   et2.getText().toString().length() > 0 &&
                                   et3.getText().toString().length() > 0 )

                           {
                               SharedPreferences spf = getSharedPreferences("newprfs", Context.MODE_PRIVATE);
                               SharedPreferences.Editor spe = spf.edit();
                               spe.putString("uname",sp.getSelectedItem().toString());
                               spe.putString("password",et1.getText().toString());
                               spe.putString("mobile",et2.getText().toString());
                               spe.putString("dept",et3.getText().toString());

                               spe.commit();

                               finish();
                               Intent i = new Intent(Login.this,Welcome.class);
                               startActivity(i);
                           }
                       }
                   }
           );
        }

this my Welcome page

SharedPreferences spf = getSharedPreferences("newprfs", Context.MODE_PRIVATE);
        SharedPreferences.Editor spe = spf.edit();
        String spv = spf.getString("uname","");
        String et1v = spf.getString("password","");
        String et2v = spf.getString("mobile","");
        String et3v = spf.getString("dept","");

        if((spe != null) && (spv !=null) && (et1v != null) && (et2v != null) && (et3v != null) &&
                (sp != null) && (et1 != null) && (et2 != null) && (et3 != null)){
        new Handler().postDelayed(new Runnable(){
            @Override
            public void run() {
                    Intent i = new Intent(Welcome.this,MainActivity.class);
                    startActivity(i);
                    Welcome.this.finish();
            }
        }, SPLASH_DISPLAY_LENGTH);
    }
        else{
            Intent i = new Intent(Welcome.this,Login.class);
            startActivity(i);
            Welcome.this.finish();
        }
    }       

The problem is that it always showing a blank screen. I tried to check with Log.d("@@@@@@@@@@@@@@Loginpage@@@@@@@@@@@@@@@@"); so i recognised to its loading always in Login page and if condition is not working.. can any one suggest me whats wrong in if Condition..?

Update Here Its not Problem with Shared Prefs.. But here Its the Problem with Condition...

I the If Condition without Entering Values it should not move to Next... and Also if values already Defined previously it should Move to next page...

So Its moving to next page and In next page Also Same happening Because I have given same condition and Again Its moving to Login page again. So it stays on the Single page and I am getting blank Screen.

ravi
  • 474
  • 1
  • 3
  • 11
  • you have to define sharedpreferences above the if() condition – Sudheesh R Oct 10 '17 at 08:36
  • I have already Defined Its not a Issue..Here the problem with Condition Here I can Get login details I checked with toast but the problem with that Condition... – ravi Oct 10 '17 at 09:09
  • I am using `shared preference`, in Login page button will not working after entering the all details...please any answer me its urgent – ravi Oct 18 '17 at 10:11

1 Answers1

0

You need to try with contains(String Key)

Try this spv.contains("uname") instead of (spv !=null) or spv.equals("") I also Faced this issue Better To use Hashmap... but this works for your Case..

Whats Going On
  • 1,379
  • 6
  • 20
  • 49
  • thanks...It working i am looking for this thing only – ravi Oct 12 '17 at 06:37
  • I already answered this in the comments on 10 October of my answer 10 oktober. Doesn't matter @ravi but not very nice. – jobbert Oct 14 '17 at 17:53
  • That's Good @jobbert But Inst-ed of Giving answer in Comments.. You need to Update Your Answer And Also @Ravi is Comparing weather the vale is there or not So He must use `contains(String Key)..` – Whats Going On Oct 17 '17 at 05:39
  • here login page when i am enter the details after press the button will not working...any give the ans – ravi Oct 18 '17 at 09:47