我创建了一个具有正常ID +密码以及Facebook登录按钮的登录页面。我将SharedPrefrerence字符串保存为"选项"值为0或1以识别用户是否正常登录或Facebook按钮。对于注销,我的布局在操作栏中有一个图标,当单击时会打开另一个片段,其中有一个用于注销的按钮。问题是当用户通过Facebook按钮登录而不是转到具有注销按钮的片段时,用户会在单击图标(在操作栏中)时立即注销。它没有使用注销按钮转到另一个片段。
Fragment with logout button
public class Prof_settings extends Fragment{
private SQLiteHandler db;
private SessionManager session;
Button logout;
public static final String PREFS_NAME = "MyApp_Settings";
String option;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View v = LayoutInflater.from(getActivity()).inflate(R.layout.prof_settings, null);
// SqLite database handler
db = new SQLiteHandler(getActivity().getApplicationContext());
// session manager
session = new SessionManager(getActivity().getApplicationContext());
SharedPreferences settings = getActivity().getSharedPreferences(PREFS_NAME, getActivity().MODE_PRIVATE);
option = settings.getString("option", "");
if (!session.isLoggedIn()) {
logoutUser();
}
logout = (Button)v.findViewById(R.id.logout);
logout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
logoutUser();
}
});
return v;
}
private void logoutUser() {
if(option.equals("1")){ //for facebook login
LoginManager.getInstance().logOut();
Intent login = new Intent(getActivity(), LoginActivity.class);
startActivity(login);
getActivity().finish();
}else if(option.equals("0")){ // for normal login
session.setLogin(false);
db.deleteUsers();
// Launching the login activity
Intent intent = new Intent(getActivity(), LoginActivity.class);
startActivity(intent);
getActivity().finish();
}
}
}

First Layout Code
public class ProfileFrag extends Fragment{
ImageView pic, grid, list, calender, map;
LinearLayout LL_grid, LL_list;
GridView GV;
TextView profile_name;
private SQLiteHandler db;
private SessionManager session;
String option, name;
Bundle bundle;
public static final String PREFS_NAME = "MyApp_Settings";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = LayoutInflater.from(getActivity()).inflate(R.layout.frag_profile, null);
profile_name = (TextView)v.findViewById(R.id.profile_name);
SharedPreferences settings = getActivity().getSharedPreferences(PREFS_NAME, getActivity().MODE_PRIVATE);
option = settings.getString("option", "");
if(option.equals("1")){
name = settings.getString("name", "");
profile_name.setText(name);
}else if(option.equals("0")){
// SqLite database handler
db = new SQLiteHandler(getActivity().getApplicationContext());
// session manager
session = new SessionManager(getActivity().getApplicationContext());
// Fetching user details from SQLite
HashMap<String, String> user = db.getUserDetails();
name = user.get("name");
// Displaying the user details on the screen
profile_name.setText(name);
}
////////////////////////////////// Settings Icon Function Starts ////////////////////////////////////////////////////////////////////////////////
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.activity_main_actions, menu);
super.onCreateOptionsMenu(menu,inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case R.id.settings:
Prof_settings newFragment = new Prof_settings();//global variable
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.popBackStack();
FragmentTransaction ft =fragmentManager.beginTransaction();
ft.replace(R.id.fragment_place, newFragment,"Order");
ft.commit();
default:
break;
}
return true;
}
/////////////////////////////////////////////// Settings Icon Function Ends ////////////////////////////////////////////////////////////////////
}
&#13;
答案 0 :(得分:0)
我通过在SharedPreference选项条件中插入isLoggedin()方法解决了这个问题。
setExact(int, long, PendingIntent)