2012-07-04 24 views
1

當我按下我的主屏幕上的按鈕退出我的應用程序它沒有發生。它正在與我的登錄窗口循環,所以我如何退出我的應用程序?新來這裏的android 是我的主屏幕上的代碼如何退出我的應用程序在Android

public class Home extends Activity implements OnClickListener { 
    Button btnLinkToRegister; 
    Button btnLinkTologin; 
    Button btnLinkToCompanyRegister; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.welcome); 

     btnLinkToRegister = (Button) findViewById(R.id.btnLinkToRegister); 
     btnLinkToCompanyRegister=(Button)findViewById(R.id.btnLinkToCompanyRegister); 
     btnLinkTologin=(Button) findViewById(R.id.btnLinkTologin); 
     btnLinkToRegister.setOnClickListener(this); 
     btnLinkTologin.setOnClickListener(this); 
     btnLinkToCompanyRegister.setOnClickListener(this); 
    }   

public void onClick(View v) 
{ 
switch(v.getId()) 
{ 

case R.id.btnLinkToRegister: 
Intent i = new Intent(getBaseContext(), Registration.class); 
startActivity(i); 
break; 

case R.id.btnLinkTologin: 
    Intent j = new Intent(getBaseContext(), Login.class); 
    startActivity(j); 
    break; 

case R.id.btnLinkToCompanyRegister: 
    Intent k = new Intent(getApplicationContext(), CompanyRegister.class); 
    startActivity(k); 
} 

} 


} 

這裏是登錄活動

public class Login extends Activity implements OnClickListener 
{ 

Button mLogin; 
Button mRegister; 

EditText muname; 
EditText mpassword; 

DBHelper DB = null; 
public String status=""; 



    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     mRegister = (Button)findViewById(R.id.register); 
     mRegister.setOnClickListener(this); 

     mLogin = (Button)findViewById(R.id.login); 
     mLogin.setOnClickListener(this); 


    } 


public void onClick(View v) 
{ 
switch(v.getId()) 
{ 

case R.id.register: 
    Intent i = new Intent(getBaseContext(), Registration.class); 
    startActivity(i); 
    break; 

case R.id.login: 

    muname = (EditText)findViewById(R.id.Ledituname); 
    mpassword = (EditText)findViewById(R.id.Leditpw); 

    String username = muname.getText().toString(); 
    String password = mpassword.getText().toString(); 



    if(username.equals("") || username == null) 
    { 
    Toast.makeText(getApplicationContext(), "Please enter User Name", Toast.LENGTH_SHORT).show(); 
    } 
    else if(password.equals("") || password == null) 
    { 
    Toast.makeText(getApplicationContext(), "Please enter your Password", Toast.LENGTH_SHORT).show(); 
    } 
    else 
    { 
    boolean validLogin = validateLogin(username, password, getApplicationContext()); 

    if(validLogin) 
    {   
     if(status.equals("s")) 
     { 
    Intent in = new Intent(getBaseContext(), JSHomePage.class); 
    in.putExtra("UserName", muname.getText().toString()); 
    startActivity(in); 
     } 
     else if(status.equals("c")) 
     { Intent in = new Intent(getBaseContext(), CompView.class); 
     in.putExtra("UserName", muname.getText().toString()); 
     startActivity(in);} 
    } 
    } 
    break; 

} 

} 


private boolean validateLogin(String username, String password, Context baseContext) 
{ 
    DB = new DBHelper(getBaseContext()); 
    SQLiteDatabase db = DB.getReadableDatabase(); 

    String[] columns = {"_id","status"}; 

    String selection = "username=? AND password=?"; 
    String[] selectionArgs = {username,password}; 

    Cursor cursor = null; 
    try{ 

    cursor = db.query(DBHelper.Login_Table, columns, selection, selectionArgs, null, null, null); 

    startManagingCursor(cursor); 

    } 
    catch(Exception e) 

    { 
    e.printStackTrace(); 
    } 
int numberOfRows = cursor.getCount(); 

    if(numberOfRows <= 0) 
    { 

    Toast.makeText(getApplicationContext(), "User Name and Password miss match..\nPlease Try Again", Toast.LENGTH_LONG).show(); 
    Intent intent = new Intent(getBaseContext(), Login.class); 
    startActivity(intent); 
    return false; 
    } 

    cursor.moveToNext(); 
    status = cursor.getString(cursor.getColumnIndex("status")); 
    startManagingCursor(cursor); 
    return true; 

} 
public void onBackPressed() 
    { 
     Intent i = new Intent(Login.this, Home.class); 
     startActivity(i); 
    } 

public void onDestroy() 
{ 
    super.onDestroy(); 
    DB.close(); 
} 
} 
+0

請過帳你的登錄活動。 – Sam

回答

1

我想你努力實現看起來像的東西:

HomeActivity - > buttonClicked - > LoginActivity - > BackPressed - > HomeActivity - > BackPressed - >退出你的應用程序

在你的情況,刪除onBackPressed覆蓋方法sh應該做的竅門(Android管理自動後退鍵活動之間的導航)。 您的循環可能來自startActivity的調用,這會將新的活動添加到堆棧而不是宕機。如果你重寫onBackPressed(),你可能需要調用super,不確定。

+0

我的問題已解決.. :-) – sambot