Encountering Unreachable statement in switch case while implementing Firebase Auth - java

"Statement not reachable under the switch case for signing out using the button in menu item. "
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case R.id.sign_out_menu:
AuthUI.getInstance().signOut(this); //NOT REACHABLE
return true;
default:
return super.onOptionsItemSelected(item);
}
}

write this line at the end of method
"return super.onOptionsItemSelected(item);"

Remove the first line in the method onOptionsItemSelected
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item); // Remove this line
switch(item.getItemId()){
}
}
Because you have a return statement in your code, everything under the 1st return statement which is not part of the switch is not reachable

you can use either return super.onOptionsItemSelected(item); or just true; after ending switch case, don't forget break; statement too.
int id = item.getItemId();
switch (id) {
case R.id.id1:
Fragment1 fragment1 = new Fragment1();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(((ViewGroup)getView().getParent()).getId() , fragment1);
fragmentTransaction.commit();
break;
case R.id.id2:
Fragment2 fragment2 = new Fragment2();
FragmentManager fragmentManager1 = getFragmentManager();
FragmentTransaction fragmentTransaction1 = fragmentManager1.beginTransaction();
fragmentTransaction1.replace(((ViewGroup)getView().getParent()).getId() , fragment2);
fragmentTransaction1.commit();
break;
}
return true;

Related

When I click the bottom navigation item more times the fragment loading again

This is my code I have created a bottom navigation view and I am connected the items to fragment separately. When I click the the navigation item more time the fragment load again again.
I don't know what is the problem here.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
bottomNav.setOnNavigationItemSelectedListener(navListener);
}
private BottomNavigationView.OnNavigationItemSelectedListener navListener =
new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment SelectedFragment=null;
FragmentManager fm=getSupportFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
ft.commit();
switch (item.getItemId()) {
case R.id.navigation_home:
setTitle("Playing11");
HomeFragment fragment = new HomeFragment();
ft.replace(R.id.fragment_container, fragment);
return true;
case R.id.navigation_dashboard:
HomeFragment1 fragment1 = new HomeFragment1();
ft.replace(R.id.fragment_container, fragment1);
return true;
case R.id.navigation_notifications:
setTitle("Safe11");
HomeFragment2 fragment2 = new HomeFragment2();
ft.replace(R.id.fragment_container, fragment2);
return true;
case R.id.navigation_notification:
setTitle("More");
HomeFragment3 fragment3 = new HomeFragment3();
ft.replace(R.id.fragment_container, fragment3);
return true;
}
return false;
}
};
}
Try to change your code like this:
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
switch (item.getItemId()) {
case R.id.menu_home:
HomeFragment homeFragment = new HomeFragment();
fragmentTransaction.replace(R.id.frameContentPlace, homeFragment, "HomeFragment").addToBackStack("HomeFragment");
break;
case R.id.menu_profile:
HomeFragment1 fragment1 = new HomeFragment1();
fragmentTransaction.replace(R.id.frameContentPlace, fragment1).addToBackStack("HomeFragment1");
break;
case R.id.menu_bookings:
HomeFragment2 fragment2 = new HomeFragment2();
fragmentTransaction.replace(R.id.frameContentPlace, fragment2).addToBackStack("HomeFragment2");
break;
}
fragmentTransaction.commit();
return true;
}
When you adding fragment use tag. So write
ft.replace(R.id.fragment_container, fragment, "MyFragment);
instead of
ft.replace(R.id.fragment_container, fragment);
And check if this fragment is visible for not loading this fragment again
switch (item.getItemId())
{
case R.id.navigation_home:
HomeFragment fragment = (HomeFragment)getFragmentManager().findFragmentByTag("MyFragment");
if (fragment != null && !fragment.isVisible())
{
setTitle("Playing11");
HomeFragment fragment = new HomeFragment();
ft.replace(R.id.fragment_container, fragment);
return true;
}
}

getting wrong fragment onclick of bottomnavigation

Hi i'm making an app where i'm using fragments with bottom navigation however when i click on home button it doesnt load home fragment instead it loads the second fragment and moreover when i start the app home fragment doesnt load by default i know it may sound naive but i'm having this problem plaese if someone may guide me
my code for fragment navigation
public class MainActivity extends AppCompatActivity {
FrameLayout frameLayout;
FragmentManager fragmentManager;
Fragment fragment;
BottomNavigationView bottomNavigationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//BottomNavigationView b=findViewById(R.id.bottom_nav);
fragmentManager = getSupportFragmentManager();
if(findViewById(R.id.frame_container)!=null){
if(savedInstanceState!=null){
return;
}
/*HomeFragment homeFragment = new HomeFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_container, homeFragment,null);
transaction.addToBackStack(null);
/* Comment this line and it should work!*/
//transaction.addToBackStack(null);
//transaction.commit();
}
bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_nav);
//final SeekBar sb = (SeekBar) findViewById(R.id.sb);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment fragment = null;
switch (item.getItemId()){
case R.id.navigation_home:
fragment = new HomeFragment();
loadFragment(fragment);
case R.id.navigation_feed:
fragment = new FeedFragment();
loadFragment(fragment);
return true;
case R.id.navigation_event:
fragment = new EventsFragment();
loadFragment(fragment);
return true;
case R.id.navigation_nearby:
fragment = new NearbyFragment();
loadFragment(fragment);
return true;
case R.id.navigation_profile:
fragment = new ProfileFragment();
loadFragment(fragment);
return true;
}
return false;
}
});
}
private void loadFragment(Fragment fragment) {
// HomeFragment homeFragment = new HomeFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_container, fragment,null);
transaction.addToBackStack(null);
/* Comment this line and it should work!*/
//transaction.addToBackStack(null);
transaction.commit();
// load fragment
//HomeFragment homeFragment = new HomeFragment();
}
}
You just forgot to return true; inside case R.id.navigation_home: check it
Try this
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment fragment = null;
switch (item.getItemId()){
case R.id.navigation_home:
fragment = new HomeFragment();
loadFragment(fragment);
return true;
case R.id.navigation_feed:
fragment = new FeedFragment();
loadFragment(fragment);
return true;
case R.id.navigation_event:
fragment = new EventsFragment();
loadFragment(fragment);
return true;
case R.id.navigation_nearby:
fragment = new NearbyFragment();
loadFragment(fragment);
return true;
case R.id.navigation_profile:
fragment = new ProfileFragment();
loadFragment(fragment);
return true;
}
return false;
}
});

Android: How to switch from Fragment to Main Activity?

My MainActivity is the Google Maps Activity and I have 2 fragments called "LoginForm" and "appSettings". I can switch from the MainActivity to the 2 fragments, but I can't switch back to MainActivity. I have tried fragment.addToBackStack() but it doesn't do anything. Any idea? Here is the code from the Navigation Drawer.
public void selectedDrawerItem(MenuItem menuItem){
Class fragmentClass = null;
FrameLayout frameLayoutMap = (FrameLayout) findViewById(R.id.main_container_login);
FragmentManager fragmentManager = getSupportFragmentManager();
// For every new Fragment, add in Switch statement.
switch(menuItem.getItemId()) {
case R.id.nav_geomap:
fragmentClass = MapsActivity.class;
break;
case R.id.nav_login:
Log.d(TAG, "Accessed fragment: menuItem nav_login");
//frameLayoutMap.removeAllViews();
fragmentClass = LoginForm.class;
break;
case R.id.nav_settings:
Log.d(TAG, "Accessed fragment: menuItem nav_settings");
// frameLayoutMap.removeAllViews();
fragmentClass = appSettings.class;
break;
}
try
{
fragment = (Fragment) fragmentClass.newInstance();
}
catch (Exception e){
e.printStackTrace();
}
fragmentManager.beginTransaction().replace(R.id.map, fragment).commit();
menuItem.setChecked(true);
setTitle(menuItem.getTitle());
mDrawerLayout.closeDrawers();
}
try this
public void addPage(Fragment fragment) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.content_main, fragment);
ft.addToBackStack(null);
ft.commit();
}
public void forwardPage(final Fragment fragment) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_main, fragment);
ft.addToBackStack(null);
ft.commit();
}

How to use onBackPressed in BottonNavigationView?

How can I use onBackPressed in BottonNavigationView to select an item? For example, if I press item 2 I go to fragment number 2, but when I push back, I go back to fragment number 1 and item 2 is still selected, item 1 is not selected which is the one corresponding to fragment 1 ...
This is my code
private BottomNavigationViewEx.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
switch (item.getItemId()) {
case R.id.mapa:
transaction.replace(R.id.content, new MapFragment());
getSupportFragmentManager().popBackStack();
transaction.commit();
return true;
case R.id.taxi:
transaction.replace(R.id.content, new TaxiFragment());
transaction.addToBackStack(null);
getSupportFragmentManager().popBackStack();
transaction.commit();
return true;
case R.id.help:
transaction.replace(R.id.content, new HelpFragment());
transaction.addToBackStack(null);
getSupportFragmentManager().popBackStack();
transaction.commit();
return true;
case R.id.go:
transaction.replace(R.id.content, new GoFragment());
transaction.addToBackStack(null);
getSupportFragmentManager().popBackStack();
transaction.commit();
return true;
}
return false;
}
};
}
UPDATE
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mapa);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.content, new MapFragment()).commit();
BottomNavigationViewEx bottomNavigationViewEx = (BottomNavigationViewEx) findViewById(R.id.bottomViewBar);
bottomNavigationViewEx.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
BottomNavigationViewHelper.setupBottomNavigationView(bottomNavigationViewEx);
bottomNavigationViewEx.setSelectedItemId(R.id.mapa);
MenuItem item = bottomNavigationViewEx.getMenu().getItem(0);
if (!item.isChecked()) item.setChecked(true);
}
private BottomNavigationViewEx.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
switch (item.getItemId()) {
case R.id.mapa:
transaction.replace(R.id.content, new MapFragment());
getSupportFragmentManager().popBackStack();
transaction.commit();
return true;
case R.id.taxi:
transaction.replace(R.id.content, new TaxiFragment());
transaction.addToBackStack(null);
getSupportFragmentManager().popBackStack();
transaction.commit();
return true;
case R.id.help:
transaction.replace(R.id.content, new HelpFragment());
transaction.addToBackStack(null);
getSupportFragmentManager().popBackStack();
transaction.commit();
return true;
case R.id.go:
transaction.replace(R.id.content, new GoFragment());
transaction.addToBackStack(null);
getSupportFragmentManager().popBackStack();
transaction.commit();
return true;
}
return false;
}
};
#Override
public void onBackPressed () {
if (mCurrentItem > 0) {
mCurrentItem--;
showFragment (mCurrentItem);
} else {
super.onBackPressed();
}
}
private void showFragment(int currentItem) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
switch (currentItem) {
case 0:
transaction.replace(R.id.content, new MapFragment());
getSupportFragmentManager().popBackStack();
transaction.commit();
break;
case 1:
transaction.replace(R.id.content, new TaxiFragment());
getSupportFragmentManager().popBackStack();
transaction.commit();
break;
case 2:
transaction.replace(R.id.content, new HelpFragment());
getSupportFragmentManager().popBackStack();
transaction.commit();
break;
case 3:
transaction.replace(R.id.content, new GoFragment());
getSupportFragmentManager().popBackStack();
transaction.commit();
break;
}
}
}
I have this now, but I don't know if is correct
Keep a variable to denote which Fragment is your current. Inside onBackPressed, check what is the current Fragment, and decide where do you want to navigate to.
#Override
public void onBackPressed() {
if (mCurrentItem > 0) {
mCurrentItem--;
showFragment(mCurrentItem);
} else {
super.onBackPressed();
}
}
Your showFragment(mCurrentItem) can show different page based on the value, or however you'd like it to.
private void showFragment(int currentItem) {
switch (currentItem) {
case 0:
transaction.replace(R.id.content, new MapFragment());
getSupportFragmentManager().popBackStack();
transaction.commit();
break;
case 1:
...
}
}
Then inside your showFragment(currentItem) method, add this code to select the item on BottomNavigationView:
MenuItem item = mBottomNavigationView.getMenu().getItem(mCurrentItem);
if (!item.isChecked()) item.setChecked(true);
Solve my problem by adding this line inside the class of my corresponding fragment in the onCreateView method, so when pressing back the item is selected.
BottomNavigationViewEx navigation =
getActivity().findViewById(R.id.YourNavigationId);
Menu drawer_menu = navigation.getMenu();
MenuItem menuItem;
menuItem = drawer_menu.findItem(R.id.YourItemId);
if(!menuItem.isChecked())
{
menuItem.setChecked(true);
}

How to Handle onback on fragment's webview in android

In my fragment activity am using the webview.when i am clicking on the hyperlink on the webview its redirecting to the specified url its fine.but the problem is when i am clicking on back it is redirected to the home fragment instead of the previos page.Can anyone help me how to redirect to the previous page on the webview.Below is my code in the mainactivity where i am handling the fragment onback press events.
private void selectItem(int position) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
// Locate Position
Fragment fragment = null;
switch (position) {
case 0:
//ft.replace(R.id.content_frame, frag,"home");
fragment=new Fagmentmain();
/*startActivity(new Intent(MainActivity.this,Category.class));*/
break;
case 1:
//ft.replace(R.id.content_frame, webfrag,"web");
fragment=new WebFragment();
/*Intent intent=new Intent(MainActivity.this,News.class);
intent.putExtra("pos",0);
startActivity(intent);*/
//startActivity(new Intent(MainActivity.this,News.class));
break;
case 2:
//ft.replace(R.id.content_frame, aboutfrag,"about");
Intent intent1=new Intent(MainActivity.this,News.class);
intent1.putExtra("pos",1);
startActivity(intent1);
break;
case 3:
Intent intent2=new Intent(MainActivity.this,News.class);
intent2.putExtra("pos",2);
startActivity(intent2);
break;
}
/*ft.commit();
mDrawerList.setItemChecked(position, true);
setTitle(title[position]);
mDrawerLayout.closeDrawer(mDrawerList);*/
if(fragment != null){
FragmentManager fragmanager=getSupportFragmentManager();
fragmanager.beginTransaction().replace(R.id.content_frame,fragment).addToBackStack("").commit();
mDrawerList.setItemChecked(position, true);
setTitle(title[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
}
#Override
public void onBackPressed() {
super.onBackPressed();
FragmentManager fm = getSupportFragmentManager();
//FragmentTab1 frag=(FragmentTab1)getSupportFragmentManager().findFragmentById(arg0)
if (fm.getBackStackEntryCount() > 0) {
Log.e("back stack entry", fm.getBackStackEntryCount() + "");
fm.popBackStack();
}
}
solved the problem by adding following code in my Fragment
webview.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack() ){
webview.goBack();
return true;
}
return false;
}
});

Categories