0

I'm trying to pass an intent to check a session name and if it matches "Admin" then I want a button to become visible.

When the toast is called the session name is completely blank

I have sent intents from the Login class like so:

 public void onClick(View v) {
                final String username = usernameET.getText().toString();
                final String password = passwordET.getText().toString();    
                boolean success = db.Login(username, password);       
                boolean adminSuccess = db.adminLogin(username, password);
                if(success)
                {
                    Log.d("login", "user logged");                  
                    session = true;                     
                    Intent i = new Intent(Login.this, MainActivity.class);
                    Intent a = new Intent(Login.this, MyAccount.class);
                    Intent h = new Intent(Login.this, Homepage.class);
                    i.putExtra("loginSuccess", session);
                    i.putExtra("sessionName", username);      
                    a.putExtra("sessionName", username);
                    h.putExtra("sessionName", username);
                    startActivity(a);
                    startActivity(i); 
                    startActivity(h);
                }
                else if (adminSuccess)
                {
                    Log.d("login", "admin logged");                 
                    session = true;                     
                    Intent i = new Intent(Login.this, MainActivity.class);
                    Intent a = new Intent(Login.this, MyAccount.class);
                    Intent h = new Intent(Login.this, Homepage.class);
                    i.putExtra("loginSuccess", session);
                    h.putExtra("loginSuccess", session);
                    i.putExtra("sessionName", username);      
                    a.putExtra("sessionName", username);
                    h.putExtra("sessionName", username);
                    startActivity(a);                  
                    startActivity(h);
                    startActivity(i); 
                }
                else
                {
                    Log.d("login", "user not logged");
                }
            }

I then call it in my MainActivity.java (which works) and in my Homepage.java (which doesn't)

Like so in the Homepage's onCreate() method:

//Check if admin is logged in for editting
        final Intent intent = getIntent();  
        boolean loginSuccess = intent.getBooleanExtra("loginSuccess", true);
        String sessionName = intent.getStringExtra("sessionName");
        Toast.makeText(Homepage.this, sessionName, Toast.LENGTH_LONG).show();       
        if (sessionName == "Admin")
            {                   
                editBtn.setVisibility(View.VISIBLE);
            }
        } 

It works in the same way in my MainActivity

Am I meant to do it in the onResume() method instead? Not too sure why it isn't passing

Matt Murphy
  • 265
  • 2
  • 11
  • What's the point of trying to start all of those activities? Why not save the variables in some kind of persistent storage then access them wherever you need? – codeMagic Apr 06 '16 at 17:16
  • I'm going to change it to sharedpreferences after I've got this working - just need it showing the button for now, don't worry :) – Matt Murphy Apr 06 '16 at 17:16
  • 1
    Then just do that now. It's not going to take any longer than it would to debug an issue that will be irrelevant once you figure it out ;) – codeMagic Apr 06 '16 at 17:17
  • Will do - any idea why this intent doesn't work though? – Matt Murphy Apr 06 '16 at 17:18
  • Probably because [you are comparing Strings wrong](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) `sessionName == "Admin"` should be `sessionName.equals("Admin")` – codeMagic Apr 06 '16 at 17:22

0 Answers0