Stop rebuilding fragment again and again - android

In my application I am giving navigation drawer by these lines of code
switch (menuItem.getItemId()) {
case R.id.home:
menuItem.setChecked(true);
drawerLayout.closeDrawer(GravityCompat.START);
fragment = new HomeFragment();
fragmentManager.beginTransaction().replace(R.id.list_view_container, fragment).commit();
return true;
case R.id.current_event:
menuItem.setChecked(true);
drawerLayout.closeDrawer(GravityCompat.START);
return true;
case R.id.college_events:
menuItem.setChecked(true);
drawerLayout.closeDrawer(GravityCompat.START);
return true;
case R.id.national_events:
menuItem.setChecked(true);
drawerLayout.closeDrawer(GravityCompat.START);
return true;
case R.id.workshops:
menuItem.setChecked(true);
drawerLayout.closeDrawer(GravityCompat.START);
return true;
case R.id.trainings:
menuItem.setChecked(true);
Toast.makeText(MainActivity.this, "Launching " + menuItem.getTitle().toString(), Toast.LENGTH_SHORT).show();
drawerLayout.closeDrawer(GravityCompat.START);
return true;
case R.id.companies:
menuItem.setChecked(true);
Toast.makeText(MainActivity.this, menuItem.getTitle().toString(), Toast.LENGTH_SHORT).show();
drawerLayout.closeDrawer(GravityCompat.START);
return true;
case R.id.talents:
menuItem.setChecked(true);
Toast.makeText(MainActivity.this, menuItem.getTitle().toString(), Toast.LENGTH_SHORT).show();
drawerLayout.closeDrawer(GravityCompat.START);
return true;
case R.id.games:
menuItem.setChecked(true);
Toast.makeText(MainActivity.this, menuItem.getTitle().toString(), Toast.LENGTH_SHORT).show();
drawerLayout.closeDrawer(GravityCompat.START);
return true;
case R.id.submission:
menuItem.setChecked(true);
Toast.makeText(MainActivity.this, menuItem.getTitle().toString(), Toast.LENGTH_SHORT).show();
drawerLayout.closeDrawer(GravityCompat.START);
return true;
case R.id.about:
menuItem.setChecked(true);
Toast.makeText(MainActivity.this, menuItem.getTitle().toString(), Toast.LENGTH_SHORT).show();
drawerLayout.closeDrawer(GravityCompat.START);
return true;
}
Each time new selection goes on fragments get rebuilt again and again I just want to just pause each fragment at its last state and wake up on their selection without getting rebuilt.

you should use something like this:
case 1:
if(getSupportFragmentManager().findFragmentByTag("ranking")==null) {
getSupportFragmentManager().beginTransaction().add(R.id.fragment_holder,
new RankingFragment(), "ranking").
addToBackStack("ranking").
setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.commit();
} else {
getSupportFragmentManager().popBackStack("ranking", 0);
}
break;

Related

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;
}
});

Navigation drawer is not closing even after closeDrawers()

I am creating a app with navigation drawer which is used to navigate through activities.
Here's my code or drawer
private void initInstances() {
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
drawerToggle = new ActionBarDrawerToggle(busr.this, drawerLayout, R.string.hello_world, R.string.hello_world);
drawerLayout.setDrawerListener(drawerToggle);
drawerLayout.closeDrawers();
navigation = (NavigationView) findViewById(R.id.navigation_view);
navigation.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
int id = menuItem.getItemId();
switch (id) {
case R.id.navigation_item_1:
startActivity(new Intent(busr.this, MainActivity.class));
break;
case R.id.navigation_item_2:
startActivity(new Intent(busr.this, aff.class));
break;
case R.id.navigation_item_3:
startActivity(new Intent(busr.this, webs.class));
break;
case R.id.navigation_item_4:
startActivity(new Intent(busr.this, admnActivity.class));
break;
case R.id.navigation_item_5:
startActivity(new Intent(busr.this, busr.class));
break;
case R.id.navigation_item_6:
startActivity(new Intent(busr.this, trng.class));
break;
case R.id.navigation_item_7:
startActivity(new Intent(busr.this, prospct.class));
break;
case R.id.navigation_item_8:
startActivity(new Intent(busr.this, erp.class));
break;
case R.id.navigation_item_9:
startActivity(new Intent(busr.this, result.class));
break;
}
return false;
}
});
try this: For closing the drawerlayout
case R.id.navigation_item_1:
if (drawerLayout.isDrawerOpen(Gravity.START))
drawerLayout.closeDrawer(Gravity.START);
startActivity(new Intent(busr.this, MainActivity.class));
break;
or
case R.id.navigation_item_1:
drawerLayout.closeDrawers();
startActivity(new Intent(busr.this, MainActivity.class));
break;

Hiding activitiy / options menu on different fragments

The idea is to temporarily hide an option menu for a specific fragment. If the user is signed in, the system should hide the menu.
In all other fragments the menu should be implemented and enabled.
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.menu_main_home:
if (MainActivity.isSignedIn() == true){
(signedIn == false)
forwardToWelcomeFragment();
} else {
Toast.makeText(this, "You are not logged in.",
Toast.LENGTH_SHORT).show();
forwardToLoginFragment();
}
return true;
case R.id.menu_main_settings:
Toast.makeText(this, "", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_main_info:
Toast.makeText(this, "", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_main_signout:
Toast.makeText(this, "", Toast.LENGTH_SHORT).show();
setSignedIn(false);
forwardToLoginFragment();
return true;
default:
return super.onOptionsItemSelected(item);
}

How to use on click listener on menu items?

Instead of using toast , i want to use on click listener in the menu items, and can we use fragments in this case
this is the following code in which i want to add on click, so i can open in a new activity
// Initiating Menu XML file (menu.xml)
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.layout.menu, menu);
return true;
}
/**
* Event Handling for Individual menu item selected
* Identify single menu item by it's id
* */
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.menu_bookmark:
// Single menu item is selected do something
// Ex: launching new activity/screen or show alert message
Toast.makeText(AndroidMenusActivity.this, "Bookmark is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_save:
Toast.makeText(AndroidMenusActivity.this, "Save is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_search:
Toast.makeText(AndroidMenusActivity.this, "Search is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_share:
Toast.makeText(AndroidMenusActivity.this, "Share is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_delete:
Toast.makeText(AndroidMenusActivity.this, "Delete is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_preferences:
Toast.makeText(AndroidMenusActivity.this, "Preferences is Selected", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Yes you can do that by simply adding an Intent against each menu item within switch case. Have a look on the below snippet for your reference:
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.menu_bookmark:
// Ex: launching new activity/screen or show alert message
Intent intent = new Intent(yourActivity.this, NextActivity.class)
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.menu_bookmark:
Intent intent = new Intent(currentactivity.this, toactivity.class)
startActivity(intent);
return true;
case R.id.menu_save:
Intent intent = new Intent(currentactivity.this, toactivity.class)
startActivity(intent);
return true;
case R.id.menu_search:
Intent intent = new Intent(currentactivity.this, toactivity.class)
startActivity(intent);
return true;
case R.id.menu_share:
Intent intent = new Intent(currentactivity.this, toactivity.class)
startActivity(intent);
return true;
case R.id.menu_delete:
Intent intent = new Intent(currentactivity.this, toactivity.class)
startActivity(intent);
return true;
case R.id.menu_preferences:
Intent intent = new Intent(currentactivity.this, toactivity.class)
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Intent
You have to use intent for moving from one screen to another
Intent intent = new Intent(currentactivity.this,towhichactivityyouwantmove.class)
startActivity(intent);

Show Popup Menu on menu key event

I have problem in creating pop-up menu on menu key event. I don’t understand how to pass required parameter to popmenu constructor. If any know what is the problem in my code then please suggest.
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU)
{
View v=getCurrentFocus();
PopupMenu popupMenu = new PopupMenu(this,v);
popupMenu.inflate(R.menu.poupup_menu);
popupMenu.setOnMenuItemClickListener(
new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_red:
Toast.makeText(context,"red",2000).show();
break;
case R.id.menu_blue:
Toast.makeText(context,"red",2000).show();
break;
case R.id.menu_green:
Toast.makeText(context,"red",2000).show();
break;
}
return true;
}
});
popupMenu.show();
// ...
return true;
} else {
return super.onKeyUp(keyCode, event);
}
}
}
thanks guys for answering...I got perferct solution to handle android menus..Override onCreateoption menu method,in that get menu instance and inflate our customize menu..and handle menu click event.
/* Initiating Menu XML file (menu.xml) */
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.layout.menu, menu);
return true;
}
/**
* Event Handling for Individual menu item selected
* Identify single menu item by it's id
* */
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.menu_bookmark:
// Single menu item is selected do something
// Ex: launching new activity/screen or show alert message
Toast.makeText(AndroidMenusActivity.this, "Bookmark is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_save:
Toast.makeText(AndroidMenusActivity.this, "Save is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_search:
Toast.makeText(AndroidMenusActivity.this, "Search is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_share:
Toast.makeText(AndroidMenusActivity.this, "Share is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_delete:
Toast.makeText(AndroidMenusActivity.this, "Delete is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_preferences:
Toast.makeText(AndroidMenusActivity.this, "Preferences is Selected", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}

Categories

Resources