我有一个名为main的活动,另一个是登录和设置。如果我登录,我保存我的用户ID,并从下次应用程序打开我的家庭活动。在家庭活动我有按钮名为settings.In设置有一个按钮命名注销,然后通过按下它将用户带到SignIn活动页面。但问题是,当我按下后退按钮应用程序应该关闭所有活动,因为我已经调用system.exit(1)而不是它带我回家活性
SharedPreferences pref;
// Editor for Shared preferences
Editor editor;
// Context
Context _context;
// Shared pref mode
int PRIVATE_MODE = 0;
// Sharedpref file name
private static final String PREF_NAME = "AndroidHivePref";
// All Shared Preferences Keys
private static final String IS_LOGIN = "IsLoggedIn";
// User name (make variable public to access from outside)
public static final String KEY_ID = "id";
// Email address (make variable public to access from outside)
// public static final String KEY_EMAIL = "email";
// Constructor
public SessionManager(Context context){
this._context = context;
pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor = pref.edit();
}
public void createLoginSession(String id){
// Storing login value as TRUE
editor.putBoolean(IS_LOGIN, true);
// Storing name in pref
editor.putString(KEY_ID, id);
// Storing email in pref
// editor.putString(KEY_EMAIL, email);
// commit changes
editor.commit();
}
/**
* Check login method wil check user login status
* If false it will redirect user to login page
* Else won't do anything
* */
public boolean checkLogin(){
// Check login status
if(!this.isLoggedIn()){
return false;
}
else
{
return true;
}
}
/**
* Get stored session data
* */
public HashMap<String, String> getUserDetails(){
HashMap<String, String> user = new HashMap<String, String>();
// user name
user.put(KEY_ID, pref.getString(KEY_ID, null));
// user email id
//user.put(KEY_EMAIL, pref.getString(KEY_EMAIL, null));
// return user
return user;
}
/**
* Clear session details
* */
public void logoutUser(){
// Clearing all data from Shared Preferences
editor.clear();
editor.commit();
// After logout redirect user to Login Activity
Intent i = new Intent(_context, SignInActivity.class);
// Closing all the Activities
// i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//
// // Add new Flag to start new Activity
// i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// Staring Login Activity
_context.startActivity(i);
}
/**
* Quick check for login
* **/
// Get Login State
public boolean isLoggedIn(){
return pref.getBoolean(IS_LOGIN, false);
}
TextView forgetPassword,newAccount,guest;
EditText emailEditText,passwordEditText;
private Button signIn;
private int user_check=1;
private Editor editor;
SessionManager session;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_signin);
session = new SessionManager(getApplicationContext());
emailEditText=(EditText)this.findViewById(R.id.emailEditText);
passwordEditText=(EditText)this.findViewById(R.id.passwordEditText);
//forgetPassword= (TextView) findViewById(R.id.signup);
newAccount= (TextView) findViewById(R.id.signup);
signIn = (Button) findViewById(R.id.signInButton);
guest=(TextView) findViewById(R.id.guest);
newAccount.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i=new Intent(SignInActivity.this, SignUpActivity.class);
startActivity(i);
}
});
signIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SignInModel signInModel = new SignInModel();
String email = emailEditText.getText().toString().trim();
String password = passwordEditText.getText().toString().trim();
if(FormHelper.isValidEmail(email) )
{
signInModel.setEmail(email);
signInModel.setPassword(password);
//Utility.showMessage(SignInActivity.this, signInModel.getEmail()+" "+signInModel.getPassword());
if(haveNetworkConnection()==true)
{
new SignInTask(SignInActivity.this,signInModel).execute();
}
else
{
Toast.makeText(SignInActivity.this, "Check Your Internet Connection", Toast.LENGTH_LONG).show();
}
// if(signInModel.getStatus()==true)
// {
//// Intent i=new Intent(SignInActivity.this, HomeActivity.class);
//// i.putExtra("user_check",1 );
//// startActivity(i);
// //finish();
//
// }
// else
// {
// Toast.makeText(SignInActivity.this, "Enter valid email and password",Toast.LENGTH_LONG ).show();
//
// }
//
//
// //finish();
//
}
else
{
Toast.makeText(SignInActivity.this, "Enter email and password",Toast.LENGTH_LONG ).show();
}
}
});
guest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// editor.putInt(user_check, 0);
// editor.apply();
Intent i=new Intent(SignInActivity.this, HomeActivity.class);
i.putExtra("user_check",0 );
startActivity(i);
finish();
}
});
}
public void signInDataLoaded(SignInModel signIn)
{
//Utility.showMessage(this, signIn.getStatus()+" "+signIn.getUserId());
if(signIn.getStatus()==true)
{
//Utility.showMessage(this, "Login Successfull! User: "+signIn.getUserId());
//SessionManager.getCurrrentSession().setCurrentUser(signIn.getUserId());
// Intent i=new Intent(SignInActivity.this, HomeActivity.class);
// startActivity(i);
// finish();
session.createLoginSession(signIn.getUserId());
Intent i=new Intent(SignInActivity.this, HomeActivity.class);
i.putExtra("user_check",1 );
startActivity(i);
finish();
}
else
{
Toast.makeText(SignInActivity.this, "Enter valid email and password",Toast.LENGTH_LONG ).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
return super.onOptionsItemSelected(item);
}
private boolean haveNetworkConnection() {
boolean haveConnectedWifi = false;
boolean haveConnectedMobile = false;
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] netInfo = cm.getAllNetworkInfo();
for (NetworkInfo ni : netInfo) {
if (ni.getTypeName().equalsIgnoreCase("WIFI"))
if (ni.isConnected())
haveConnectedWifi = true;
if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
if (ni.isConnected())
haveConnectedMobile = true;
}
return haveConnectedWifi || haveConnectedMobile;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) { //Back key pressed
//Things to Do
AlertDialog.Builder alertDialog = new AlertDialog.Builder(SignInActivity.this);
// Setting Dialog Title
alertDialog.setTitle("Confirm Exit...");
// Setting Dialog Message
alertDialog.setMessage("Are you sure you want to exit?");
// Setting Icon to Dialog
alertDialog.setIcon(drawable.cancel);
// Setting Positive "Yes" Button
alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
// Write your code here to invoke YES event
// Toast.makeText(getApplicationContext(), "You clicked on YES", Toast.LENGTH_SHORT).show();
System.exit(1);
}
});
// Setting Negative "NO" Button
alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to invoke NO event
//Toast.makeText(getApplicationContext(), "You clicked on NO", Toast.LENGTH_SHORT).show();
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
return true;
}
return super.onKeyDown(keyCode, event);
}
signinorout是我的退出按钮
signInOrOut.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// SessionManager.getCurrrentSession().setCurrentUser(null);
session.logoutUser();
Intent i=new Intent(SettingsActivity.this, SignInActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// Add new Flag to start new Activity
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
finish();
}
});
在我的家庭活动中,我还没有完成活动,但我已经在设置中清除了我以前的所有活动Intent.flag_activity_clear_top
答案 0 :(得分:0)
Intent i = new Intent(_context, SignInActivity.class);
// Closing all the Activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// Add new Flag to start new Activity
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// Staring Login Activity
_context.startActivity(i);
您应该取消注释第二行和第三行。