[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 472
  • Last Modified:

Android user login page

Hello there,

i am developing a simple login UI,were user will login and go to the main page. the problem i have is that i get exception when i try to return the user back to the login page when they enter wrong credential.it is working fine when the user enters the correct credential.i have attached the files.please help.
Login.java
MainMenu.java
0
zolf
Asked:
zolf
  • 7
  • 4
1 Solution
 
alexey_gusevCommented:
sorry for the dumb question, but why don't you check for credentials validity in Login class?
0
 
SanthanaTechnical LeadCommented:
Hi  zolf,
if(username.equals("test") && password.equals("secret"))
            {
                  setContentView(R.layout.mainmenu);

            //Radio button is available in the mainmenu.xml. So move this piece of code inside this if block
                  RadioButton order = (RadioButton) findViewById(R.id.radio_order);
                  RadioButton cusInfo = (RadioButton) findViewById(R.id.radio_customer);

                  order.setOnClickListener(new OnClickListener()
                  {
      
                        @Override
                        public void onClick(View v)
                        {
                              Intent intent = new Intent(MainMenu.this, Order.class);
                              startActivity(intent);
                        }
                  });

                  cusInfo.setOnClickListener(new OnClickListener()
                  {
      
                        @Override
                        public void onClick(View v)
                        {
                              // TODO Auto-generated method stub
                        
                        }
                  });
            }
            else
            {
                  Log.d("ZAJ","Inside ELSE Loop");
                  Intent intent = new Intent(MainMenu.this, Login.class);
                  startActivity(intent);
                  
                  //setContentView(R.layout.main);
            }




This will work if you enter wrong username & password it will launch the Loginscreen again.
But i guess it is not a correct way to implement this login screen.


1) In Login.java

LOGIN_RESULT = 100;
....
startActivityForResult(intent,LOGIN_RESULT);
.....

override the onActivityResult(int reqCode, int resCode, Intent data){
        if(LOGIN_RESULT == reqCode){
              finish();
        } else {
           username.setText("");  
           password.setText("");  
        }
}


2) In MainMenu.java


in ocCreate(){
.......

        if(!TextUtils.isEmpty(username) && !TextUtils.isEmpty(password)
            && username.equals("test") && username.equals("secret")
            ){
            setResult(RESULT_OK);
              finish();
        } else {
      
            setResult(RESULT_CANCEL);
            finish();
            return;
        }
      
      setContentView(R.layout.mainmenu);
.......
}
0
 
zolfAuthor Commented:
santhanasamy

thanks for your comments and feedbacks.
Can you please give a bried description to the implementation which you suggested. i will appreciate it.

cheers
Zolf
0
New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

 
SanthanaTechnical LeadCommented:
Hi  zolf,

In your implementation you are launching the Login activity every time if the username/ password is wrong. So every time it will create a new activity instance if the user enter wrong username/ password ,,,,,,

So i Called the "MainMenu" for result(onActivityResult(...)).  It will return the result to the "Login" activity.
If the result is success it will finish the "Login activity".Other wise it will just clear the texts that we entered previously in the username & password field...

In MainMenu activity if the username&password is correct we will send the result "RESULT_OK" as result to the Login activity and immediately we are setting the view "R.layout.mainmenu"

Other wise we will send the result "RESULT_CANCEL" as result to the Login activity  and immediately we are finishing  the MainMenu activity...
0
 
zolfAuthor Commented:
thanks for your feedbacks.it was very helpful for a beginner like me.
one question, can you please tell me now that in my MainMenu.java the if/else has changed to

if(!TextUtils.isEmpty(username) && !TextUtils.isEmpty(password)
            && username.equals("test") && username.equals("secret")
            ){
            setResult(RESULT_OK);
              finish();
        } else {
     
            setResult(RESULT_CANCEL);
            finish();
            return;
        }

where will the code inside of my previous if statement go.
0
 
zolfAuthor Commented:
Hi santhanasamy,

can you please look at my code and tell me if i am doing it correct. i am still not clear on some part of the code. see my previuos comment
Login.java
MainMenu.java
0
 
zolfAuthor Commented:
in the OnActivityResult,shouldnt it be responseCode instead of the reqCode which you mentioned in your code.please clarify
0
 
zolfAuthor Commented:
i have made some improvement to the code. but now when i clcik on the radio buttons nothing happens. what am i doing wrong.please see my code.
one question, in the Login class the onActivityResult method,in the if do i have to use setContentVIew or I have to use Intent to go to the MainMenu once the lgin is successful.??
Login.java
MainMenu.java
0
 
SanthanaTechnical LeadCommented:
Hi  zolf,

Please check this code....
It is perfectly working to me....Ehen i select the radio button it is launching the order activity...
Login.java
MainMenu.java
0
 
zolfAuthor Commented:
Hi santhanasamy

thanks for your feedback. why do we have to call finish() in here.i mean what does it do.

@Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data)
      {
            if (LOGIN_RESULT == requestCode)
            {
                  if (resultCode == RESULT_OK)
                  {
                         finish();


also i want to know the difference between calling setContentView and Intent when i want to switch to another activity.
0
 
SanthanaTechnical LeadCommented:
Hi  zolf,

If you didn't call the finish ,Login screen will be in the back ground.
If the user enter correct username and password ,it will take to the MainMenu screen.
The default implementation of the backkey will launch the previous activity.So If you you press the back key  from the MainMenu activity (without calling finish ()) , it will launch the LoginActivity.

Intent & setContentView both are not inter relatedwith each other.

setContentView  -- >  Will give the User interface (Views) to an Activity through the layout resource xml.

Intent  --> Android component communicate with each other through the asynchronous messages called Intents.
Geneally Intent object is a passive data structure will hold the abstract information of the operation need to be performed....

All the 3 android components (Activity, Service,  BroadcastReceivers) are activated through this messages called intent.

To know more about Intent refer following links..
http://www.vogella.de/articles/AndroidIntent/article.html
http://developer.android.com/guide/topics/intents/intents-filters.html
0
 
zolfAuthor Commented:
cheers mate
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 7
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now