How do we share Drawer with all the activities?
In the lister: onNavigationItemSelected of setNavigationItemSelectedListener we can get the id and navigate to it. What I am looking for is something like this:
private void initDrawerLayout() {
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
NavigationView navView = (NavigationView) findViewById(R.id.navigation_view);
navView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
Intent intent;
int id = menuItem.getItemId();
switch(id) {
case R.id.home:
case R.id.drawer_home:
// call activity instead of adding/replacing fragment
return true;
case R.id.drawer_gallery:
// call activity instead of adding/replacing fragment
intent = new Intent(MainActivity.this, GalleryActivity.class);
startActivity(intent);
return true;
case R.id.drawer_about:
// call activity instead of adding/replacing fragment
intent = new Intent(MainActivity.this, AboutActivity.class);
startActivity(intent);
return true;
...
}
...
I know I can make all the menuItems add/replace Fragment, but then handling fragments and memory management is a big pain.
Instead I want each menuItem select/click to invoke Activity. i.e. each MainMenuItem to have Activity and those will hold fragments with complex layouts.
All I want to do is have each main menu item be an Activity instead of a
Fragment.
And all these activities can share same DrawerNavigation.
Is this the recommended way? Or do we always add Fragments for NavigationDrawer item clicks??
Should I add NavigationView to BaseActivity and then extend all activities from there??
Following this new guide about Support Design lib
I found the answer using this SO answer
Extending is the right way. Just override setContentView in the right way...
I am a bit confused here as well. After finding very little information on this I tried to extend my subclass
public class NewActivity extends MainActivity{
...
}
However, this alone didn't do anything. the MainActivity has a fully functioning NavigationView that will navigate to each activity..Only thing that is left is sharing it with each activity.
Related
I want to start an intent then automatically selecting a fragment after clicking a button.
This is the function of my button.
startActivity(new Intent(AlertNotif.this, HomeActivity.class));
In my HomeActivity.class, it has a BottomNav for different fragments.
bottom_nav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
BottomNavigationView bottomNavigationView = (BottomNavigationView)findViewById(R.id.bottom_nav);
BottomNavigationViewHelper.removeShiftMode(bottomNavigationView);
switch (item.getItemId()){
case R.id.nav_map:
setFragment(mapFragment);
return true;
case R.id.nav_cctv:
setFragment(cctvFragment);
return true;
My default fragment for the HomeActivity is the mapFragment, i want to set it to cctvFragment after clicking the button, instead of the default fragment. I also tried changing the HomeActivity.class to CctvFragment.class, but it is still opening to default.
On the onCreate of the activity you are calling HomeActivity use:
navigation.selectedItemId = R.id.your_id
Where navigation is your layout in the xml for the bottomNavigationView and your id is the id of your navigation item!
All it does is call your onNavigationItemSelectListener when the activity is created.
Remember to set the onNavigationItemSelectListener before you use this code
This code is in Kotlin, I hope is not a problem
I am using Bottom Navigation View and in one of the options we display Firebase data, with a CardView.
But when I get out of this activity, and come back, either with the device back button or the activity arrow, it does not work. Does not display anything.
What could be wrong?
Prints:
Before:
After:
Code Bottom Navigation View:
BottomNavigationView bottomNavigationView =(BottomNavigationView) findViewById(R.id.bottomNavView_Bar);
BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);
Menu menu = bottomNavigationView.getMenu();
MenuItem menuItem = menu.getItem(0);
menuItem.setChecked(true);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.ic_home:
break;
case R.id.ic_explore:
Intent intentExplore = new Intent(HomeActivity.this, ExploreActivity.class);
intentExplore.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intentExplore);
break;
case R.id.ic_calendar:
Intent intentAgenda = new Intent(HomeActivity.this, AgendamentoActivity.class);
intentAgenda.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intentAgenda);
break;
case R.id.ic_person:
Intent intentuser = new Intent(HomeActivity.this, OpcoesUsuarioActivity.class);
intentuser.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intentuser);
break;
}
return false;
}
});
Note: In tests in activities without Bottom Navigation or with SlidingTabs, it works normally.
well you are using a wrong approach about the BottomNavigationView you need to use Fragment not Activities check this. About your problem when you are clicking on any item you puts your activity onPause that mean the activity still at the stack but is not visible, when clicking back you just finish the current activity and back to the last one with calling the onResume method the onCreate is not called, so put your RecyclerView work at your onResume method, or call finish() when leaving the activity.
What function should i use to open an activity when a DrawerLayout navigationView item is clicked, and do i need to make object for that activity?
Here is my code so far:
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_profile) {
// Handle the camera action
} else if (id == R.id.nav_cv) {
} else if (id == R.id.nav_subscription) {
} else if (id == R.id.nav_logout) {
}
You can open activity like this.
Intent I=new Intent(MainActivity.this,actvitytoopen.class);
StartActivity(I);
You better use fragments instead of activities and change them using FragmentManager. You can find plenty of tutorials just by searching on Google/Youtube.
You can start new activity by
Intent intent = new Intent(this,NextActivity.class);
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
for further info refer https://developer.android.com/training/basics/firstapp/starting-activity.html
I spent a lot of time trying to call a new activity by clicking on navigation drawer item . but it is virtually impossible and if it is possible in any other way I tried, then it is sure that on the called activity there will be no drawer navigation drawer then. Fragment is a part of similar activity so calling fragment is the correct way. If you are calling fragment, navigation drawer will still be there on the side pane and you would be able to select between different items of the drawer.
I want to add back navigation to toolbar. I need to get from a fragment in an activity to a specific fragment in another activity.
It looks a little like this, where every orange line means navigating to a new activity or fragment:
How do I move from fragment B to fragment A from OtherActivity?
Consider these steps:
From Activity 1 holding Fragment A , you want to directly load Fragment B in Activity 2.
Now, I am thinking first, then you press a button in Fragment A, you can directly go to Activity B.
Then it means, you can simply load Fragment B as soon as you arrive in Activity 2.
Since you are dealing with back navigation (I believe you mean the upNavigation?), you can override the following:
But watch clearly, because if you need to load an exact fragment in Activity 2, you need to know somehow:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent intent = new Intent(this, Activity2.class);
intent.putExtra("frag", "fragmentB");
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
As you can see, when you click the back arrow on the toolbar, we pass a value through our intent to identity which fragment we want to load.
Next, in your Activity2, simply get the intent extra and do a switch or an if statement:
#Override
public void onResume(){
super.onResume();
Intent intent = getIntent();
String frag = intent.getExtras().getString("frag");
switch(frag){
case "fragmentB":
//here you can set Fragment B to your activity as usual;
fragmentManager.beginTransaction().replace(R.id.container_body, new FragmentB()).commit();
break;
}
}
From here, you should have your Fragment B showing in Activity 2.
Now you can handle the same thing while inside Activity 2 to decide where to go when a user clicks the back home arrow!
I hope this helps you get an idea.
Note: I thought about the interface approach and realized it is not necessary since this can be done easily with this approach!
To navigate from one Activity to another Activity's Fragment, with Kotlin version 1.4.0 and, for example, calling a click listener on a button it works so:
binding.yourButton.setOnClickListener {
supportFragmentManager.beginTransaction().replace(R.id.yourLayout, NameOfYourFragment()).commit()
}
Use this code to change your fragment
fragmentManager.beginTransaction().replace(R.id.container_body, new FragmentC()).commit();
and to show navigation on custom toolbar add
Toolbar toolbar = (Toolbar) rootView.findViewById(R.id.toolbar);
((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true)
((AppCompatActivity)getActivity()).getSupportActionBar().setHomeButtonEnabled(true);
I am trying out the navigation drawer (slide menu) given in this tutorial.
The difference with above link and mine is that instead of calling fragments I am trying to call the activity. When the app opens I am not able to see the Navigation drawer menu I can see only the action bar with HOME activity opened.
Here is the code that I changed: (Is it necessary to have a fragment or can I use activity for my first screen in Navigation Drawer?)
mTitle = mDrawerTitle = getTitle();
navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
navMenuIcons = getResources().obtainTypedArray(R.array.nav_drawer_icons);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
navDrawerItems = new ArrayList<NavDrawerItem>();
navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(1, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(2, -1),true, "200"));
navMenuIcons.recycle();
mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
adapter = new NavDrawerListAdapter(getApplicationContext(), navDrawerItems);
mDrawerList.setAdapter(adapter);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.drawer,
R.string.drawer_open,
R.string.drawer_close
)
{
public void onDrawerClosed(View view)
{
getActionBar().setTitle(mTitle);
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView)
{
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null)
{
displayView(0);
}
}
private class SlideMenuClickListener implements
ListView.OnItemClickListener
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
displayView(position);
}
}
private void displayView(int position)
{
switch (position)
{
case 0:
//fragment = new HomeFragment();
Intent intent = new Intent(this, Home.class);
startActivity(intent);
return;
case 1:
//fragment = new FindPeopleFragment();
Intent intent1 = new Intent(this, Profile.class);
startActivity(intent1);
break;
case 2:
//fragment = new PhotosFragment();
Intent intent2 = new Intent(this, Details.class);
startActivity(intent2);
break;
default:
break;
}
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
How do I fix this to show Navigation drawer on my Home Activity?
Update:
I even tried the following option given in this link:
How can I call one of my activity using navigation drawer ? but I am still not getting the navigation slide menu.
You can't do that...
Navigation drawer is a layout from an activity and you cant show an activity inside another activity, you need to use a fragments for this!
An activity is a screen, you cant show a screen inside another screen, but a fragment may be a component of a screen, and you can inflate a fragment inside a container of an activity and then show to the user.
if you wan't do this, you can create a abstract activity and inherit, but you will not have a a activity in a fragment, you will have a multiple activities with each one with your own navigation drawer.
If you look at the sample documentation for Android Navigation drawer, it is explicitely defined to use fragments instead of activities to load different 'pages' from the navigation bar. As the app lifecycle for Android goes, fragments are easier to load and produce less impact on the Android system. they can also be easily set into the background and be replaced by other different fragments.
Having said that, your best approach would be to convert your activities into fragments and use them to load the different pages you need in your app. It is not such a difficult task, and I will show you how:
Change all activities to fragments
public class nameOfFragment extends Fragment { }
Use the onCreateView method instead of onCreate
public View onCreateView() {
View view = inflater.inflate(R.layout.activity_main, container, false);
//any other elements in that view need to be included here in this manner.
Button rate = (Button) rootView.findViewById(R.id.rate);
return view;
}
These changes should be enough to change your activities into fragments.
You need to do the same for all fragments which are accessed through your navigation bar.
Please post your code for the activities you are using if you need any further help.
Hope this helps :)
You've got the code to build and display a navigation bar right? Put your code above into a base activity class. Every activity that you want to access the navigation bar from should then inherit from that base class.
Google's IO 2014 app does the same thing, take a look at their source here.
When you do this, you will need to find a way to maintain the drawer's state as you transition to the next activity. I'm not sure how to do that, but you'll probably find the answer in the source code for the IO app.
If you have to have single instance of drawer, then you have to go by fragments.
If you have no choice to go other than with activities, define a base class for all your activites(say BaseActivity). And in BaseActivity - you can do the necessary coding to integrate drawer. Each activity extending BaseActivity will now show up drawer menu (but the instances will be different)
Now you need to use the toolbar. Android has made appcompat library to introduce material design in older versions of android as well. You need to use compile dependency of appcompat.
Change the style.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
</style>
</resources>
Then we will choose to have no old action bar. After then we make a contract between navigation drawer and toolbar. So we need not to use old actionbar now.
You can refer the code below:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff6d7fe2"
app:contentInsetEnd="0dp"
app:contentInsetStart="0dp"
></android.support.v7.widget.Toolbar>