Android SharedPreference - TabHost 问题
我正在开发具有两个不同 tabhost 的 Android 应用程序:Main 和 Child。 在主选项卡主机中,我有 5 个不同的选项卡,最后一个选项卡打开了我有登录页面的新活动。我想在登录活动中创建一个布尔类型 isLoggedIn 并将 true 或 false 传递给主选项卡栏,因为我想更改数字如果用户登录,我将有 5 个选项卡,如果没有,我将有 4 个选项卡。那么,有什么建议如何解决这个问题?
更新:
现在我正在使用这段代码。在用户 LogIn.class 中我使用:
SharedPreferences isLogged = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = isLogged.edit();
editor.putBoolean("isLoggedIn", isLoggedIn);
editor.commit();
在 MainActivity 中我使用几乎相同的代码:
SharedPreferences isLogged = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = isLogged.edit();
editor.putBoolean("isLoggedIn", false);
editor.commit();
并且我检查这样的两个状态:
if(editor.putBoolean("isLoggedIn", false) != null){
// show 5 tabs
}else
{
// show 4 tabs;
但是当我打开我的应用程序时,我得到 5 个选项卡,甚至之前检查用户的状态。 知道如何解决这个问题吗? }
I'm working on Android application which have two different tabhost : Main and Child.
In Main tabhost I have 5 different tabs and the last one opens new activity where I have login page.I want to create a boolean type isLoggedIn in login activity and pass true or false to the Main Tab bar,because I want to change the number of tabs.If user is logged in I'll have 5, if not I'll have 4 tabs.So any suggestions how to code this problem?
Update :
For now I'm using this code.In user LogIn.class I use :
SharedPreferences isLogged = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = isLogged.edit();
editor.putBoolean("isLoggedIn", isLoggedIn);
editor.commit();
in MainActivity i use almost the same code :
SharedPreferences isLogged = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = isLogged.edit();
editor.putBoolean("isLoggedIn", false);
editor.commit();
And I check for two states like this :
if(editor.putBoolean("isLoggedIn", false) != null){
// show 5 tabs
}else
{
// show 4 tabs;
But when I open my application I get 5 tabs, even before check the status of user.
Any idea how to fix that?
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您使用 not with putBoolean 检查共享首选项中的值
,并且不必对其设置 null,如果共享首选项中不存在名称“isLoggedIn”,它将返回 false,这就是第二个参数是 for ,如果找不到名称,则返回默认值。据我了解,您需要以下代码。
此代码将检查“isLoggedIn”值,如果该值为 true,则显示 5 个选项卡,如果登录为 false,则显示 4 个选项卡。
至于另一个问题,您在主活动中用 false 更新值,不要这样做,只有在用户注销后才这样做。因此,从代码中删除以下两行。
you check for the value in shared preferences with
not with putBoolean, and you don't have to put a null against it, if the name "isLoggedIn" is not present in shared preference, it will return false, that is what the second argument is for , to return a default value if it can't find the name. as i understand you need the following code.
This code will check for "isLoggedIn" value , and will show 5 tabs if the value is true and 4 if the logged in is false.
As for the another problem, you are updating the value with false in the main activity, don't do that, do that only after the user logs out. so remove the following 2 lines from your code.
使用整个应用程序共有的sharedPreference。
Use sharedPreference, which is common to whole application.