I am trying to implement a navigation drawer which switches among fragments and activities. Whenever I move to a activity window, the hamburger icon along with the navigation toolbar gets hidden which is why I cannot get back to my navigation drawer. How to resolve the issue?
This is my navigation code:
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch(item.getItemId()){
case R.id.nav_profile:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new ProfileFragment()).commit();
break;
case R.id.nav_my_events:
Intent intent = new Intent(getBaseContext(), Main2Activity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
break;
case R.id.nav_settings:
Toast.makeText(this,"Settings..", Toast.LENGTH_LONG).show();
break;
case R.id.nav_logout:
Toast.makeText(this,"You logged out..", Toast.LENGTH_LONG).show();
break;
}
Related
I have a bottom navigation bar with 4 fragments and 1 activity inside another activity(in which all fragment will be displayed). I want my first fragment to be displayed on the starting of the activity(in which all fragment will be displayed) along with the matching item of bottom navigation bar. My fragment 1 is getting displayed on starting but with wrong item of the bottom navigation bar.
This is what, I am getting on starting. Selected item should be Home(middle)
I have this under OnCreate
btmNav = findViewById(R.id.btmnav);
btmNav.setOnNavigationItemSelectedListener((navListner));
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new RecViewFragment()).commit();
and this outside Oncreate
private BottomNavigationView.OnNavigationItemSelectedListener navListner = new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.navprofile:
getSupportFragmentManager().beginTransaction().
replace(R.id.fragment_container,new Fragment2()).commit();
break;
case R.id.navmap:
Intent intent = new Intent(MainActivityBuses.this, MapActivity.class);
startActivity(intent);
break;
case R.id.navhome:
getSupportFragmentManager().beginTransaction().
replace(R.id.fragment_container,new Fragment2()).commit();
break;
case R.id.navmybus:
getSupportFragmentManager().beginTransaction().
replace(R.id.fragment_container,new Fragment3()).commit();
break;
case R.id.navinfo:
getSupportFragmentManager().beginTransaction().
replace(R.id.fragment_container,new Fragment4()).commit();
break;
}
return true;
}
i got the answer,
just need to add this
btmNav.getMenu().findItem(R.id.navhome).setChecked(true);
below this
btmNav = findViewById(R.id.btmnav);
btmNav.setOnNavigationItemSelectedListener((navListner));
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new RecViewFragment()).commit();
I'm working on this code
private void setupViews(){
frameLayout = (FrameLayout) findViewById(R.id.frame_id);
bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_nav_id);
getSupportFragmentManager().beginTransaction().replace(R.id.frame_id,new ProfileFragment()).commit();
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
int navID = menuItem.getItemId();
switch (navID){
case R.id.home:
getSupportFragmentManager().beginTransaction().replace(R.id.frame_id,new HomeFragment()).commit();
break;
case R.id.search:
getSupportFragmentManager().beginTransaction().replace(R.id.frame_id,new SearchFragment()).commit();
break;
case R.id.profile:
getSupportFragmentManager().beginTransaction().replace(R.id.frame_id,new ProfileFragment()).commit();
break;
}
return true;
}
When I launch my app, it's automatically going on first bottom (I have 3 bottoms)
I want to change this to second bottom in navigation view. Please help me
Try this:
bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_nav_id);
bottomNavigationView.setSelectedItemId(R.id.search);
getSupportFragmentManager().beginTransaction().replace(R.id.frame_id,new SearchFragment()).commit();
#iamhanniballake
I call setupViews() in main activity
I have 3 botoms
-1-2-3-
when my app running open fragment with a bottomnav view
its automatically selected 3 I want to change it to 2
I created a Navigation Drawer and inserted the necessary items and their fragments.
Also, I added a item that I have to open another Activity, which code should I put that by pressing on the item do this?
For example, item id nav_menu6 ..... but it should not be linked to a fragment but open an activity ...
private void displaySelectedScreen(int itemId) {
Fragment fragment = null;
// Handle navigation view item clicks here.
switch (itemId) {
case R.id.nav_menu1:
fragment = new BlankFragment();
break;
case R.id.nav_menu2:
fragment = new BlankFragment2();
break;
case R.id.nav_menu3:
fragment = new BlankFragment3();
break;
case R.id.nav_menu4:
fragment = new BlankFragment4();
break;
case R.id.nav_menu5:
fragment = new BlankFragment5();
break;
case R.id.nav_menu6:
fragment = new BlankFragment6();
break;
}
https://developer.android.com/training/basics/firstapp/starting-activity.html
Read this about starting a new activity.
Basically, the key is the intent.
Intent intent = new Intent(this, YourActivityName.class);
startActivity(intent);
or in a shortened form:
startActivity(new Intent(this, YourActivityName.class));
I have implemented a ´NavigationView´ which has a few menu options that when user selects one, it changes the "main fragment" view to anothers...
My problem is, I'd like that when the user presses the back button, unless he is on the "main fragment" the screen returns to the "main fragment", otherwise close the app (regular onBackPressed() behaviour when there is no activity or backStack).
What I've done so far:
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.debitsNavId:
getSupportFragmentManager().beginTransaction().replace(R.id.main_fragment_container, new MainFragment(), "main_fragment").commit();
break;
case R.id.friendsNavId:
getSupportFragmentManager().beginTransaction().replace(R.id.main_fragment_container, new FriendFragment(), "friend_fragment").commit();
break;
case R.id.notificationsNavId:
getSupportFragmentManager().beginTransaction().replace(R.id.main_fragment_container, new RequestFragment(), "request_fragment").commit();
break;
case R.id.configurationsNavId:
Intent i = new Intent(MainActivity.this,
SettingsActivity.class);
startActivity(i);
break;
case R.id.logoutNavId:
logout();
break;
default:
return false;
}
menuItem.setChecked(true);
drawer_layout.closeDrawers();
return false;
}
The problem is, if the user navigates from 'FriendFragment' to 'RequestFragment' or vice-versa, the fragment that gets "backstacked" is the previous one (either FriendFragment or RequestFragment) and I'd like to still be "MainFragment".
How can I achieve that ?
Hope I made myself clear and thanks for your time !
So basically what you would need to do is that when the MainFrag is visible set a boolean to true and if you navigate away set it to false.
Then override the onBackPressed, in your main activity, like the following method:
#Override
public void onBackPressed() {
if (!isMainFragVisible) {
getSupportFragmentManager().beginTransaction().replace(R.id.main_fragment_container, new MainFragment(), "main_fragment").commit();
}
}
You can set the isMainFragVisible to true in this method:
case R.id.debitsNavId:
getSupportFragmentManager().beginTransaction().replace(R.id.main_fragment_container, new MainFragment(), "main_fragment").commit();
isMainFragVisible = true
break;
And set it to false in any other navigation method.
do it this way
getSupportFragmentManager().beginTransaction().addToBackStack(fragment.getClass().getSimpleName()).replace(R.id.main_fragment_container, new MainFragment(), "main_fragment").commit();
I implemented Navigation Drawer in my app. It's just a sample app, auto-generated Navigation Drawer fragments and activities from Android studio. I'm starting an activity from a section list item like this:
public void onSectionAttached(int number) {
switch (number) {
case 1:
mTitle = getString(R.string.pocetna);
break;
case 2:
mTitle = getString(R.string.oglasna_ploca);
break;
case 3:
mTitle = getString(R.string.e_novine);
break;
case 4:
mTitle = getString(R.string.portal);
break;
case 5:
mTitle = getString(R.string.raspored);
startActivity(new Intent(this, RasporedWebView.class));
break;
}
}
When I use the back button, could I get back to let's say case 1 or even MainActivity (closing navigation drawer), because when I call the activity, and go back it returns to a blank activitity (or w/e), and then I must click back button once more. I tried searching for solutions, but couldn't find any.
Thanks in advance.
Refer this Docs.
Then add the below code to do Back button in Activity
ActionBar actionBar;
actionBar=getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}