Niraj Kumar
Niraj Kumar

Reputation: 41

How to make condition for Activity in android?

Scenario is that I have a splash screen which reads a password from device. If found password then go to Main Activity, else, go to other activity.

I have problem in making condition. Kindly help me out for example:

SharedPreferences oPref = getActivity().getSharedPreferences("RegistrationData",Context.MODE_PRIVATE);
String pin = oPref.getString("mobileNumebr", " ");

Upvotes: 0

Views: 133

Answers (4)

Mahesh Babariya
Mahesh Babariya

Reputation: 4570

Try this code

        String pin = oPref.getString("mobileNumebr", " ");

        new CountDownTimer(3000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {

        }

        @Override
        public void onFinish() {
            if (pin.equals("")) {
                startActivity(new Intent(SplashPage.this, LoginActivity.class));
            } else {
                startActivity(new Intent(SplashPage.this, HomePage.class));
            }
            finish();
         }
        }.start();

Upvotes: 2

varun bhardwaj
varun bhardwaj

Reputation: 1522

You should use the TextUtills class of android and check like this:

SharedPreferences oPref = getActivity().getSharedPreferences("RegistrationData",Context.MODE_PRIVATE);

String pin = oPref.getString("mobileNumebr", "");//default value should be empty
if(TextUtills.isEmpty(pin)){
//Go to main activity
}else {
//Go to login Activity
}

Thanks

Upvotes: 2

Jagjit Singh
Jagjit Singh

Reputation: 1969

Try this.

String pin = oPref.getString("mobileNumebr", "");

if(pin.equalsIgnoreCase(""))
{
go to other activity
}
  else
  {
go to main activity
}

Hope it helps

Upvotes: 2

Ragesh Ramesh
Ragesh Ramesh

Reputation: 3520

Try this out

      String pin=String pin = oPref.getString("mobileNumebr", "");
      if(pin!=null && !pin.isEmpty()){
            --go to main activity--
       }else{
            -- go to other activity--
          }

Upvotes: 0

Related Questions