0

I'm creating an application to which I've added a Botton Navigation View but I don't know if there is any way that it appears in all the Activity or if on the contrary I would have to specify it in each of them to appear, as well as adding all its functionality on each screen, since this last option I see a little cumbersome.

The other question is for the button Home of this bar, I have added a button that is home to return to the main Activity each time I pulse, in the way that I have done has been with an Intent that goes to that screen itself, but I do not know if there is another method:

bottomNavigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {

            switch (menuItem.getItemId()) {

                case R.id.btnHome:

                    Intent intent = new Intent(MainActivity.this, MainActivity.class);
                    startActivity(intent);

                    break;
Ludiras
  • 338
  • 3
  • 20

1 Answers1

0

Check out how to use Fragment in Android. You don't have to open a new activity each time the button in your BottomNavigationView is clicked, instead, you use fragments, so only the selected part in activity is changed. Check out the following tutorial to learn more about Fragments. See the "dynamic creation" of fragments.

How to use Fragments in Android.

In your case, there'll be code of FragmentTransaction where you're launching a new Activity using Intent. You'll be replacing only a certain portion of the screen(between your Toolbar and `BottomNavigationView).

Vedprakash Wagh
  • 3,595
  • 3
  • 12
  • 33
  • Thanks, I'll take a look at it! – Ludiras Jun 02 '19 at 20:00
  • Hey, wait, don't check that one out. It's adding fragments in xml. It's not much helpful in the long run or what you want to do right now. I've added the link to new tutorial. Please check that out. – Vedprakash Wagh Jun 02 '19 at 20:13
  • I had looked for a tutorial on Youtube that did just the second thing you sent me :) Now I have another doubt: I used to have the code of my Activity Home in my MainActivity, but now I have the Button Navigation View and the Home is a Fragment, how can I pass the code I had to that home? I am quite new in this of programming in Android and in general... hahahahaha – Ludiras Jun 02 '19 at 20:56
  • Yeah since you didn't know about fragments, I figured you're new to Android. And converting the Activity cde to Fragment is veeery simple thing, [You'll find more here](https://stackoverflow.com/questions/21205732/converting-activity-into-fragment) – Vedprakash Wagh Jun 02 '19 at 21:01
  • Okay, I got it. Now I have another problem. I had a Splash Screen where my logo would appear for two seconds and change to the Home screen, but now when I indicate that the Home screen is the Home Fragment, the application closes, do I have to change anything? – Ludiras Jun 02 '19 at 21:10
  • It must be unable to switch to HomeScreen using intent as its a fragment now. You'll have to switch to the activity which is holding these fragments from your SplashScreen. Right now you must be switching to fragments. – Vedprakash Wagh Jun 02 '19 at 21:19
  • You have a lot to learn. Learn to find errors in the code using logcat. I cannot answer your question about crash unless I can see the error; – Vedprakash Wagh Jun 02 '19 at 21:29
  • These are the last lines of the logcat 02 23:08:20.592 1493-1493/com.isaac.appet D/AndroidRuntime: Shutting down VM 2019-06-02 23:08:20.596 1493-1493/com.isaac.appet E/AndroidRuntime: FATAL EXCEPTION: main Process: com.isaac.appet, PID: 1493 android.content.ActivityNotFoundException: Unable to find explicit activity class {com.isaac.appet/com.isaac.appet.HomeFragment}; have you declared this activity in your AndroidManifest.xml? – Ludiras Jun 02 '19 at 21:42
  • I've been reading and I've seen that I have to make my Splash Screen point to my Activity that contains Fragment and that activity launches HomeFragment, I'm going to look for some of that. – Ludiras Jun 02 '19 at 21:50
  • The error currently says there's no activity called HomeFragment as you converted it to fragment. You have to remove that from Manifest, and point the Intent to correct activity. – Vedprakash Wagh Jun 02 '19 at 21:56