Note that there are some explanatory texts on larger screens.

plurals
  1. PONot able to implement boolean logic properly
    primarykey
    data
    text
    <p>Here is what im trying to achieve: I have ActioBar and I have a menu item named Login over the ActionBar. When this login menu item is clicked it adds a new tab in the actionbar and loads the fragment with the login_layout inside the container of the parent activity.And if i click any other tab than the login tab disappears.And it can only reappear if the login menu item is clicked again. Once the login has been successful i want to change the title of the menu item to Log Out.And now after setting title of the Login menu item to Log out if i click it it should not add any tabs on the actiobar as user is already logged in.</p> <p>I have a working code upto the part where user has been succesfully logged in and thus he title of the log in menu item has been changed to log out.But the problem im experiencing is that now when the user clicks Log Out it still adds the tab and shows the log in fragment.</p> <p>Easyway of doing this is to menuItem.getTitle() and restrict the action based on the current title of the menuItem.But i dont think this is a good way to accomplish this.So now i tried to use flag "loginMenuItemFlag" and tried to solve the problem but no success.I have been able to make it work upto the point where it changes the title of the login menu item to log out but when the user clicks the log out it adds/shows the login tab again which i dont want.Can anyone please tell me what im doing wrong and point me in the right direction.Thank you.</p> <p>Below is the code of the class where im creating menuItem and adding Tabs,removing Tabs,changing flag, changing the title of the menuitem based on flag value:</p> <pre><code>public class HomeActivity extends SherlockFragmentActivity { String mCurFilter; static boolean flag,loginMenuItemFlag; LoginScreenFragment loginFragment; HomeFragment homeFragment; CartFragment cartFragment; MyFragment myFragment; FragmentTransaction ft, ft2; ActionBar actionbar; MenuItem loginItem; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.home); // ActionBar actionbar = getSupportActionBar(); actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); homeFragment = new HomeFragment(); cartFragment = new CartFragment(); myFragment = new MyFragment(); loginFragment = new LoginScreenFragment(); actionbar.addTab(actionbar.newTab() .setText(R.string.home_fragment_title) .setTabListener(new MyTabsListener(homeFragment)) .setTag("HOME")); actionbar.addTab(actionbar.newTab() .setText(R.string.cart_fragment_title) .setTabListener(new MyTabsListener(cartFragment)) .setTag("CART")); actionbar.addTab(actionbar.newTab() .setText(R.string.my_fragment_title) .setTabListener(new MyTabsListener(myFragment)) .setTag("MYFragment")); if (!flag) { ft = getSupportFragmentManager().beginTransaction(); ft.add(R.id.fragment_container, homeFragment); ft.show(homeFragment); ft.add(R.id.fragment_container, cartFragment); ft.hide(cartFragment); ft.add(R.id.fragment_container, myFragment); ft.hide(myFragment); ft.add(R.id.fragment_container, loginFragment); ft.hide(loginFragment); ft.commit(); flag = true; } else { } } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putInt("tab", getSupportActionBar() .getSelectedNavigationIndex()); } @Override public boolean onCreateOptionsMenu(final Menu menu) { loginItem = menu.add(R.string.menuitem_login); loginItem.setTitle(R.string.menuitem_login); loginItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); loginItem.setOnMenuItemClickListener(new OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { if(!loginMenuItemFlag){ Tab loginTab = actionbar.newTab() .setText(R.string.login_fragment_title) .setTabListener(new MyTabsListener(loginFragment)) .setTag(R.string.login_label); actionbar.addTab(loginTab); actionbar.selectTab(loginTab); loginItem.setVisible(false); loginItem.setTitle(R.string.login_label); //loginMenuItemFlag = false; } else{ loginItem.setTitle(R.string.menuitem_login); //loginMenuItemFlag = false; } return false; } }); return true; } // removes TAB at a specified position public void deleteTabAt(int tabPosition) { actionbar = getSupportActionBar(); actionbar.removeTabAt(tabPosition); } //setting Login menu Item to visible when Login tab has been removed from the tabs public void setLoginMenuItemVisible() { loginItem.setVisible(true); } public void changeLoginMenuFlag(){ loginMenuItemFlag = false; } //chaging Log In menu item to Log out upon successful Log in public void changeLoginMenutItem(){ if(!loginMenuItemFlag){ loginItem.setTitle(R.string.menuitem_logout); loginMenuItemFlag = false; }else{ loginItem.setTitle(R.string.menuitem_login); loginMenuItemFlag= true; } } class MyTabsListener implements ActionBar.TabListener { public SherlockFragment fragment; public MyTabsListener(SherlockFragment fragment) { this.fragment = fragment; } @Override public void onTabReselected(Tab tab, FragmentTransaction ft) { System.out.println("TabReselected " + tab.getPosition()); } @Override public void onTabSelected(Tab tab, FragmentTransaction ft) { System.out.println("TabSelected " + tab.getPosition()); ft.show(this.fragment); } @Override public void onTabUnselected(Tab tab, FragmentTransaction ft) { System.out.println("TabUnselected " + tab.getPosition()); ft.hide(this.fragment); //making check if the unselected tab was Login tab(located at tab position 3),if it was a login tab than we remove it from ActionBar if (tab.getPosition() == 3) { deleteTabAt(tab.getPosition()); setLoginMenuItemVisible(); } } } </code></pre> <p>}</p> <p>Below is the code from the LoginFragment where login operation is being carried out.Following is the piece of the code which which executes after the login attempt.</p> <pre><code>@Override public void sendResponse(String response) { pd.dismiss(); System.out.println("Response in the callback: " + response); if (response != null) { FragmentActivity homeActivity = getActivity(); if (homeActivity instanceof HomeActivity) { ((HomeActivity) homeActivity).deleteTabAt(3); ((HomeActivity) homeActivity).changeLoginMenuFlag(); ((HomeActivity) homeActivity).changeLoginMenutItem(); ((HomeActivity) homeActivity).setLoginMenuItemVisible(); Toast.makeText(getActivity(), "Login Successful!", Toast.LENGTH_LONG).show(); } } else { Toast.makeText(getActivity(), "Login failed,Please try again...", Toast.LENGTH_LONG).show(); } } </code></pre> <p>Thank you.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload