当用户点击Logout时,我试图从我的应用程序注销。如果用户登录后没有关闭应用程序,如果他进行了注销,则该工作正常。然后它正常工作.Back按钮将无效登录后再次显示登录页面 但是当用户登录后关闭应用程序然后他登出登录页面时没有显示它正在显示一个空白页面给我
代码
public class AppState {
private static AppState singleInstance;
private boolean isLoggingOut;
private AppState() {
}
public static AppState getSingleInstance() {
if (singleInstance == null) {
singleInstance = new AppState();
}
return singleInstance;
}
public boolean isLoggingOut() {
return isLoggingOut;
}
public void setLoggingOut(boolean isLoggingOut) {
this.isLoggingOut = isLoggingOut;
}
}
点击退出
logout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
SharedPreferences myPrefs = getSharedPreferences("MY",
MODE_PRIVATE);
SharedPreferences.Editor editor = myPrefs.edit();
editor.clear();
editor.commit();
AppState.getSingleInstance().setLoggingOut(true);
Log.d(TAG, "Now log out and start the activity login");
Intent intent = new Intent(HomePage.this,
LoginPage.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
});
在LoginActivity中
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == MAIN_ACTIVITY_REQUEST_CODE) {
if (!AppState.getSingleInstance().isLoggingOut()) {
finish();
} else {
AppState.getSingleInstance().setLoggingOut(false);
super.onActivityResult(requestCode, resultCode, data);
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
}
请告诉我这件事我做错了什么
在您建议 Vivek Bhusal 后,我尝试使用sharedpref
首页注销点击
logout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
SharedPreferences myPrefs = getSharedPreferences("Activity",
MODE_PRIVATE);
SharedPreferences.Editor editor = myPrefs.edit();
editor.clear();
editor.commit();
//AppState.getSingleInstance().setLoggingOut(true);
setLoginState(true);
Log.d(TAG, "Now log out and start the activity login");
Intent intent = new Intent(HomePage.this,
LoginPage.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
});
private void setLoginState(boolean status) {
SharedPreferences sp = getSharedPreferences("LoginState",
MODE_PRIVATE);
SharedPreferences.Editor ed = sp.edit();
ed.putBoolean("setLoggingOut", status);
ed.commit();
}
在登录页面
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
SharedPreferences sp = getSharedPreferences("LoginState",
MODE_PRIVATE);
boolean stateValue = sp.getBoolean("setLoggingOut", false);
if (requestCode == MAIN_ACTIVITY_REQUEST_CODE) {
if (!stateValue) {
finish();
} else {
//AppState.getSingleInstance().setLoggingOut(false);
updateLoginState(false);
super.onActivityResult(requestCode, resultCode, data);
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
当我再次重启应用程序然后进行注销时,显示空白屏幕的问题仍然存在。
答案 0 :(得分:21)
我注意到您的注销按钮确实启动了登录活动,但未完成主页活动。关闭主页:
Intent intent = new Intent(HomePage.this, LoginPage.class);
startActivity(intent);
finish() // This call is missing.
要从登录页面打开它:
Intent intent = new Intent(LoginPage.this, HomePage.class);
startActivity(intent);
finish()
启动另一项活动后完成一项活动。这样,您的任务堆栈将只有一个活动,即没有回溯历史记录。查看Vivek Bhusal发布的示例应用程序。
我建议您阅读此SO question,accepted answer以及this answer以获取有关如何执行注销的更多提示以及Intent.FLAG_ACTIVITY_CLEAR_TOP
的一些注意事项,在您的情况下,并非真的有必要。
答案 1 :(得分:0)
if (!AppState.getSingleInstance().isLoggingOut()) {
你认为这句话是否正确?因为我看到当这个条件成立时你完成了活动我认为这意味着注销但是isLoggingOut应该是真的,但你检查它是否是假你叫完成。我是对的吗?
答案 2 :(得分:0)
我的一个申请中有类似的问题;但我得到了解决方案。问题是,您在类中定义了bologout的布尔值,它会在您关闭应用程序后重置。它是一个变量,它存储您的值,直到您的应用程序打开。一旦你关闭你的应用程序就会重置它,你的应用程序就不会工作了......我建议你使用Sharedpreference来存储那个值..
祝你好运使用示例应用程序进行更新:
MainActivity.java
public class MainActivity extends Activity {
SharedPreferences SM;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loginpage);
SM = getSharedPreferences("userrecord", 0);
Boolean islogin = SM.getBoolean("userlogin", false);
if(islogin){
Intent intent = new Intent(MainActivity.this, Dashboard.class);
startActivity(intent);
finish();
return;
}
Button login = (Button) findViewById(R.id.login);
final EditText ETuser = (EditText) findViewById(R.id.editText1);
final EditText ETpass = (EditText) findViewById(R.id.editText2);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
String user = ETuser.getText().toString();
String pass = ETpass.getText().toString();
if(user.equalsIgnoreCase("admin") && pass.equalsIgnoreCase("admin")){
Editor edit = SM.edit();
edit.putBoolean("userlogin", true);
edit.commit();
Intent intent = new Intent(MainActivity.this, Dashboard.class);
startActivity(intent);
finish();
}else{
Toast.makeText(getApplicationContext(), "Username/Password Invalid", Toast.LENGTH_LONG).show();
}
}
});
}
}
Dashboard.java
public class Dashboard extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button logout = (Button) findViewById(R.id.logout);
logout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
SharedPreferences SM = getSharedPreferences("userrecord", 0);
Editor edit = SM.edit();
edit.putBoolean("userlogin", false);
edit.commit();
Intent intent = new Intent(Dashboard.this, MainActivity.class);
startActivity(intent);
finish();
}
});
}
}
loginpage.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp" >
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword" />
<Button
android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="login" />
</LinearLayout>
activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello User" />
<Button
android:id="@+id/logout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginLeft="50dp"
android:layout_marginTop="57dp"
android:text="Logout" />
</RelativeLayout>
试试这个例子,希望这会对你有所帮助
答案 3 :(得分:0)
答案 4 :(得分:0)
onNewIntent()
活动中应使用singleTop
。
除此之外,我没有看到startActivityForResult()
对登录页面/活动的调用。
空白页表示您的活动未设置其视图(使用setContentView()
)。
我建议您将“退出”检查和setContentView()
移至登录页面/活动的onResume()
。
答案 5 :(得分:0)
此代码用于从应用
退出 Intent logoutintent = new Intent(this, LoginActivity.class);
startActivity(logoutintent);
SharedPreferences loginSharedPreferences;
loginSharedPreferences = getSharedPreferences(
LoginActivity.MyPREFERENCES, Context.MODE_PRIVATE);
Editor editor = loginSharedPreferences.edit();
editor.putString("UniqueId", "");
editor.commit();
finish();
答案 6 :(得分:0)
案例R.id.drawer_item_logout://注销按钮的名称
AlertDialog.Builder builder=new AlertDialog.Builder(Home.this); //Home is name of the activity
builder.setMessage("Do you want to exit?");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
finish();
Intent i=new Intent();
i.putExtra("finish", true);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // To clean up all activities
//startActivity(i);
finish();
}
});
builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert=builder.create();
alert.show();
break;