how to disable a menu item when fragments are used - android

i want to disable the menu option when i launch the fragments but when i go back to the home screen i want the option to be visible.i tried to set visibility to true in onresume but it gives an null pointer exception
public boolean onNavigationItemSelected(MenuItem paramMenuItem)
{
FragmentManager localFragmentManager = getSupportFragmentManager();
WebViewFragment localWebViewFragment = new WebViewFragment();
ContactUsFragment contactus = new ContactUsFragment();
Bundle localBundle = new Bundle();
switch (paramMenuItem.getItemId())
{
case R.id.signin:localFragmentManager.beginTransaction().replace(R.id.frame_replace, new SignInFragment()).addToBackStack("signIn").commit();
break;
case R.id.drawer_book_your_car:
if ((PrefUtils.getDefaults("isSignedIn", this.mContext) == null) || (PrefUtils.getDefaults("isSignedIn", this.mContext).equalsIgnoreCase("false")) || (PrefUtils.getDefaults("isSignedIn", this.mContext).equalsIgnoreCase("")))
{
Toast.makeText(this.mContext, "Please Sign In", Toast.LENGTH_LONG).show();
return true;
}
break;
case R.id.drawer_myTrip:localFragmentManager.beginTransaction().replace(R.id.frame_replace, new TripFragment()).addToBackStack("MyTrip").commit();
break;
case R.id.drawer_tariff:startActivity(new Intent(this.mContext, TariffActivity.class));
break;
case R.id.how_to_broom:
localBundle.putString("url","www.abc.com");
localWebViewFragment.setArguments(localBundle);
localFragmentManager.beginTransaction().replace(R.id.frame_replace,localWebViewFragment).addToBackStack("web").commit();
localMenuItem.setVisible(false);
break;
case R.id.drawer_FAQs:
localBundle.putString("url","www.abc.com");
localWebViewFragment.setArguments(localBundle);
localFragmentManager.beginTransaction().replace(R.id.frame_replace, localWebViewFragment).addToBackStack("web").commit();
localMenuItem.setVisible(false);
break;
case R.id.drawer_policies:
localBundle.putString("url","www.abc.com");
localWebViewFragment.setArguments(localBundle);
localFragmentManager.beginTransaction().replace(R.id.frame_replace,localWebViewFragment).addToBackStack("web").commit();
localMenuItem.setVisible(false);
break;
case R.id.contactUs:
localBundle.putString("url", "www.abc.com");
localWebViewFragment.setArguments(localBundle);
localFragmentManager.beginTransaction().replace(R.id.frame_replace,contactus).addToBackStack("web").commit();
break;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.DONUT) {
((DrawerLayout)findViewById(R.id.drawer_layout)).closeDrawer(GravityCompat.START);
}
return true;
}
public void onResume(){
super.onResume();
localMenuItem.setVisible(false);
}

Related

Call API only once with Bottom Navigatioin Fragments

I am having a bottom navigation fragment and want to call API only once, which means when the user jumps to another tab and comes back API should not be getting called. And when the user comebacks from the background state, API should be called once. I tried calling API with the onCreate method, but oncreate method calling every time when I click on the tab. On the other side, for one fragment I am sending data from MainActivity by calling the API.
Please check the code and shed some light on where am doing wrong.
studentBottomNavView.setOnNavigationItemSelectedListener
(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment selectedFragment = null;
Fragment currentFragment = getSupportFragmentManager().findFragmentById(R.id.stdcontainer);
switch (item.getItemId()) {
case R.id.navigation_std_current_course:
if (!(currentFragment instanceof StdCurrentCourse)) {
selectedFragment = StdCurrentCourse.newInstance();
}
isHome = false;
break;
case R.id.navigation_std_points:
if (currentFragment == null) {
if (!(currentFragment instanceof StdPoints)) {
selectedFragment = StdPoints.newInstance();
}
}
isHome = false;
break;
case R.id.navigation_std_home:
if (!(currentFragment instanceof StdHome)) {
selectedFragment = new StdHome(attendanceList, studentAbsenceAndDelays, todayTaskList);
}
isHome = true;
break;
case R.id.navigation_std_task_hw:
if (!(currentFragment instanceof StdTaskAndHw)) {
selectedFragment = StdTaskAndHw.newInstance();
}
isHome = false;
break;
case R.id.navigation_chat:
if (!(currentFragment instanceof StdRealTimeChat)) {
selectedFragment = StdRealTimeChat.newInstance();
}
isHome = false;
break;
}
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_layout1, selectedFragment);
transaction.commit();
return true;
}
});
You can control API Calls using a Boolean, set as false once you have received a successful response and when BaseActivity/Fragent where studentBottomNavView is called onPause set the same Boolean as true as per your requirement.

Restrict loading two same fragments

I'm using BottomNavigationView. When I click on an option twice then same fragment also loaded twice. How to prevent loading same fragment?
btnNavBar.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.action_item1:
loadFragment(fragmentManager, new HomeFragment(), "Home");
break;
case R.id.action_item2:
loadFragment(fragmentManager, new SearchFragment(), "Search");
break;
case R.id.action_item3:
loadFragment(fragmentManager, new AccountFragment(), "Account");
break;
}
return true;
}
});
public static void loadFragment(FragmentManager fragmentManager, Fragment fragment, String tag) {
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frameLayoutContainer, fragment);
fragmentTransaction.commit();
}
the easiest approach is tracking current fragment which is displayed at BottomNavigationView:
int currentTabSelected;
...
btnNavBar.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.action_item1:
if(currentTabSelected == R.id.action_item1) return true;
loadFragment(fragmentManager, new HomeFragment(), "Home");
break;
case R.id.action_item2:
if(currentTabSelected == R.id.action_item2) return true;
loadFragment(fragmentManager, new SearchFragment(), "Search");
break;
case R.id.action_item3:
if(currentTabSelected == R.id.action_item3) return true;
loadFragment(fragmentManager, new AccountFragment(), "Account");
break;
}
currentTabSelected = item.getItemId();
return true;
}
});

ActionBarActivity back button to fragment

I have problem with the back button in action bar to fragment
my code fragment:
public class Server extends Fragment {
View view;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.activity_server, container, false);
Button server = (Button) view.findViewById(R.id.status);
/** Button Check Status Server **/
server.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent myIntent = new Intent(view.getContext(), ServerStatus.class);
startActivityForResult(myIntent, 0);
getActivity().finish();
}
});
return view;
}
}
my code in activity:
public class ServerStatus extends ActionBarActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_server_status);
}
}
My code in Fragment
#Override
public void onNavigationDrawerItemSelected(int position) {
Fragment objFragment = null;
switch (position) {
case 0:
objFragment = new Account();
break;
case 1:
objFragment = new AllNews();
break;
case 2:
objFragment = new Server();
break;
case 3:
objFragment = new Account();
break;
case 4:
objFragment = new Account();
break;
case 5:
objFragment = new Account();
break;
case 6:
objFragment = new About();
break;
}
// update the main content by replacing fragments
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container,objFragment)
.commit();
}
Every time I click the back button, the program always closes. I already tried to use:
getActionBar().setDisplayHomeAsUpEnabled(true);
and this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
Can anyone help me to fix my program?
EDIT: Concerning cross-activity-navigation, you should not use startActivityForResult() in that way. Try to use startActivity() instead. If you start an activity for a result, the calling activity waits for the onActivityResult() callback and should not be finished.
In the manifest file you should declare the fragment's activity as parent activity of ServerStatus to enable back navigation. You should not need NavUtils.
If you want to enable fragment navigation within an activity, you have to add your fragment transactions to the backstack:
getFragmentManager().beginTransaction().addToBackStack(null).replace(...).commit();
Then you have to call getFragmentManager().popBackStack() in onOptionsItemSelected() to enable back navigation for the action bar:
#override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
getFragmentManager().popBackStack();
return true;
}
return super.onOptionsItemSelected(item);
}
You may have to override onBackPressed() to enable back navigation for the back button:
#override
public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount() >= 1) {
getFragmentManager().popBackStack(); // return to previous fragment
}
else {
super.onBackPressed(); // Exit application when no fragment is on the backstack
}
}

onBackPressed closing app in fragments using NavigationDrawer

I'm using NavigationDrawer with some fragments, the problem is when I'm in a fragment and hit the back button, it makes the app close, then I have to open it again, put my username and password all over again to use the app, how can I prevent that from happen?
public class NavigationMain extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//...
}
public void setFragmentList(int posicao) {
Fragment fragment = null;
switch (posicao) {
case 0:
fragment = new MainFragment();
break;
case 1:
fragment = new MensagensFragment();
break;
case 2:
fragment = new EscolasFragment();
break;
case 3:
fragment = new AutorizadasFragment();
break;
case 4:
fragment = new CadastroFragment();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
// update selected item and title, then close the drawer
navigationAdapter.resetarCheck();
navigationAdapter.setChecked(posicao, true);
layoutDrawer.closeDrawer(linearDrawer);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
}
#Override
public void onBackPressed() {
int count = getFragmentManager().getBackStackEntryCount();
if (count == 0) {
super.onBackPressed();
//additional code
} else {
getFragmentManager().popBackStack();
}
}
I think you missed to add the fragment transaction in your back stack. Try the following:
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).addToBackStack(null).commit();

Get listview clicks from sidenavigation

I have implemented the Sliding Menu in my App. Source: https://github.com/johnkil/SideNavigation
It works like it should but when I click on any item in my menu, the click wont work some reason. I added onClick listener and all that.
Code snippet:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
sideNavigationView.toggleMenu();
Toast.makeText(getApplicationContext(),( R.string.title1),
Toast.LENGTH_LONG).show();
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
#Override
public void onSideNavigationItemClick(int itemId) {
switch (itemId) {
case R.id.side_navigation_menu_item1:
Toast.makeText(getApplicationContext(),( R.string.title1),
Toast.LENGTH_LONG).show();
break;
case R.id.side_navigation_menu_item2:
Intent intent = new Intent(this, DiffAdapter.class);
this.startActivity(intent);
break;
case R.id.side_navigation_menu_item3:
invokeActivity(getString(R.string.title3), R.drawable.ic_action_storage);
break;
case R.id.side_navigation_menu_item4:
invokeActivity(getString(R.string.title4), R.drawable.ic_action_settings);
break;
case R.id.side_navigation_menu_item5:
invokeActivity(getString(R.string.title5), R.drawable.ic_launcher);
break;
default:
return;
}
finish();
}
Any help would be nice. It just wont do anything when I click on a item.
Thanks
I forgot to implement some strings, which caused bugs. Got this solved by adding:
icon = (ImageView) findViewById(android.R.id.icon);
sideNavigationView = (SideNavigationView) findViewById(R.id.side_navigation_view);
sideNavigationView.setMenuItems(R.menu.ribbon_menu);
sideNavigationView.setMenuClickCallback(this);
if (getIntent().hasExtra(EXTRA_TITLE)) {
String title = getIntent().getStringExtra(EXTRA_TITLE);
int resId = getIntent().getIntExtra(EXTRA_RESOURCE_ID, 0);
setTitle(title);
icon.setImageResource(resId);
}
to my code: onCreate ()

Categories

Resources