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));
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 using a BottomNavigationView with 5 items, 4 Fragments and 1 Activity as in the image below
When any fragment is clicked I want it to act normally getting clicked and checked, but when the "+" item is clicked I'm opening an Activity but I don't want it to be checked, so I want it to be clickable so it can open the Activity, but I don't want to be checked, because when the user return back from the activity will see it selected even though it's the wrong selected item.
How can I do this?
Here's my code:
bottomNavigationView = findViewById(R.id.bottom_nav_view);
bottomNavigationView.setOnNavigationItemSelectedListener(this);
.
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
int id = menuItem.getItemId();
Fragment fragment = null;
switch (id) {
case R.id.home_nav_menu:
fragment = new HomeFragment();
break;
case R.id.inbox_nav_menu:
fragment = new InboxFragment();
break;
case R.id.add_nav_menu:
Intent intent = new Intent(this, AddActivity.class);
startActivity(intent);
return true;
case R.id.history_nav_menu:
fragment = new HistoryFragment();
break;
case R.id.profile_nav_menu:
fragment = new ProfileFragment();
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_place_holder, fragment).commit();
return true;
}
Seeing at the source code of the listener for the bottom navigation, you just need to return false when you don't want to display item as selected.
/**
* Listener for handling selection events on bottom navigation items.
*/
public interface OnNavigationItemSelectedListener {
/**
* Called when an item in the bottom navigation menu is selected.
*
* #param item The selected item
*
* #return true to display the item as the selected item and false if the item should not
* be selected. Consider setting non-selectable items as disabled preemptively to
* make them appear non-interactive.
*/
boolean onNavigationItemSelected(#NonNull MenuItem item);
}
So you simply return false in your case R.id.add_nav_menu: and it will work.
User MenuItem#setCheckable(boolean) API in order to play with menu item's checkable state.
MenuItem menuItem = navigation.getMenu().getItem(1);
menuItem.setCheckable(false);
Try like this:
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
int id = menuItem.getItemId();
Fragment fragment = null;
switch (id) {
case R.id.home_nav_menu:
fragment = new HomeFragment();
break;
case R.id.inbox_nav_menu:
fragment = new InboxFragment();
break;
case R.id.add_nav_menu:
menuItem.setCheckable(false);
Intent intent = new Intent(this, AddActivity.class);
startActivity(intent);
return true;
case R.id.history_nav_menu:
fragment = new HistoryFragment();
break;
case R.id.profile_nav_menu:
fragment = new ProfileFragment();
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_place_holder, fragment).commit();
return true;
}
I have a MainActivity that has several fragments which are added and then shown/hidden. This is because the MainActivity uses a NavigationDrawer. Clicking on items in the drawer causes different fragments to be added (if they do not exist), or shown/hidden if they do.
My question is, how can I launch my MainActivity via an intent from a different activity, and at the same time show a specific fragment?
Would I have to pass some extra to my MainActivity and then based on that data, add/show/hide the relevant fragment? Is there another way?
When you create your Intent, you can give it an extra that determines the fragment to load.
Intent i = new Intent(this, ActivityClass.class);
i.putExtra("frgToLoad", FRAGMENT_A);
// Now start your activity
startActivity(i);
Now, inside your activity check the extra and load the right Fragment:
OnCreate(){
...
int intentFragment = getIntent().getExtras().getInt("frgToLoad");
switch (intentFragment){
case FRAGMENT_A:
// Load corresponding fragment
break;
case FRAGMENT_B:
// Load corresponding fragment
break;
case FRAGMENT_C:
// Load corresponding fragment
break;
}
}
You can use sharedPreference to achieve the goals. Save the index after you open you TAB/Fragment.
//get current tab
sharedPreferences = getSharedPreferences("MyPrefs", MODE_PRIVATE);
String position = sharedPreferences.getString("tab_opened", null);
if(position==null){
viewPager.setCurrentItem(1,true);
}else if(position=="0"){
viewPager.setCurrentItem(0,true);
}else if(position=="1"){
viewPager.setCurrentItem(1,true);
}else if(position=="2"){
viewPager.setCurrentItem(2,true);
}
}// <<<~~~ THAT IS END OF onCreate() method.
Please refer to this tutorial : OPEN THIS .
In case you are looking for something like this, hope so!
#Override
public void onNavigationDrawerItemSelected(int position) {
Fragment fragment;
FragmentManager fManager = getFragmentManager();
Bundle args = new Bundle();
args.putInt(MCConstants.ARG_SECTION_NUMBER, position);
clearAllBackStack();
FragmentTransaction fTransaction = fManager.beginTransaction();
switch (position) {
case 0:
fragment = new FirstFragment();
fragment.setArguments(args);
fTransaction.replace(R.id.container, fragment,
"FTAG").commit();
break;
case 1:
fragment = new SecFragment();
fragment.setArguments(args);
fTransaction.replace(R.id.container, fragment,
"STAG").commit();
break;
case 2:
fragment = new ThirdFragment();
fragment.setArguments(args);
fTransaction.replace(R.id.container, fragment,
"TTAG").commit();
break;
}
}
I'm using the NaviagationDrawer with the Fragment.
Now in MainActivity, I open the various fragment but one of the lines must open an Activity.
/**
* Diplaying fragment view for selected nav drawer list item
* */
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new HomeFragment();
break;
case 1:
fragment = new InFragment();
break;
case 2:
fragment = new RiFragment();
break;
case 3:
fragment = new RieFragment();
break;
case 4:
fragment = new PagesFragment();
break;
case 5:
fragment = new WhatsHotFragment();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
In case 4, I would like to open an activity as I do?
If you want to open a new activity in case 4:, you can simply start a new activity with an Intent as you normally would.
...
case 4:
Intent intent = new Intent(this, MyActivity.class);
startActivity(intent);
break;
...
You would also want to change your if/else block on the bottom to avoid logging an error when you start a new activity (since your fragment variable would be null in this case).
I have a drawer navigation with:
private void DisplayView (int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = new Fragment1();
break;
case 1:
fragment = new Fragment2();
break;
case 2:
...
From an activity I can throw that fragment (fragment2 for example)? With BeginTransaction?
Thanks
A Fragment cannot exist in it's own, eg. without an Activity. It cannot be fired up with an Intent like Activities do. You have to either create a new Activity to hold your new Fragment or replace the Fragment of your current Activity with the new one.
You cannot start fragment as an activity, they need to be added to activities.
Read more in the docs here
Also, see this