Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does JetPack use the Navigation menu in Activity

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of how JetPack uses the navigation menu in Activity, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will have something to gain after reading this article on how to use the navigation menu in JetPack Activity. Let's take a look.

Be prepared in advance, open your xml file under the file res/navigation/, click on each Fragment page, and set the corresponding title for them. Under the id on the right under Design, there is an attribute of label. Just fill in the corresponding name.

1. Use the navigation menu in Activity

The default Activity navigation menu displays the title and the return button, while the return button returns to the previous Activity, and our Navigation is in an Activity. If the current Activity is at the top of the stack, the code I learned is to let all the Fragment be in the MainActivity, because no matter how I jump, it is in the MainActivity. So now there are two tasks:

First jump to the next page and then the back button appears.

Click the back button to return to the previous Navigation page normally.

Since the second one needs to be completed on the basis of the first to see the effect, so we follow the steps above.

The original effect:

Default effect

Open the java file that hosts the Activity of Fragment. Here is MainActivity.java, and add the following code under the onCreate method:

@ Overrideprotected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager (). FindFragmentById (R.id.nav_host_fragment); NavController navController = navHostFragment.getNavController (); appBarConfiguration = new AppBarConfiguration.Builder (navController.getGraph ()). Build (); NavigationUI.setupActionBarWithNavController (this, navController, appBarConfiguration);}

Android's official acquisition of navController is a direct acquisition of Navigation.findNavController (this, R.id.nav_host_fragment); if you unfortunately write the same thing, there will be an error:

Caused by: java.lang.IllegalStateException: Activity com.example.learnjetpack.MainActivity@b516fb8 does not have a NavController set on 2131230962

I can deal with this problem just by changing it to what I did above. As for the cause, I can't explain it now, but I will make it up later. The above code is the Bar that hosts the original Activity.

The effect of Navigation hosting Activity

So the first is done, and as for the second, as long as you rewrite the logic of the return of the original Activity:

@ Overridepublic boolean onSupportNavigateUp () {NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager () .findFragmentById (R.id.nav_host_fragment); NavController navController = navHostFragment.getNavController (); return NavigationUI.navigateUp (navController, appBarConfiguration) | | super.onSupportNavigateUp ();}

Let's take a look at the final effect.

The final effect

I also encountered a problem when my MainActivity theme setting android:theme= "@ style/Theme.AppCompat.Light.NoActionBar" resulted in the following error:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.appcompat.app.ActionBar.setTitle (java.lang.CharSequence)' on a null object reference

Just change this, because the NoActionBar is set up, and the result shows that it is true that the phone will lose its temper. This is set in AndroidManifest.xml.

two。 Use Toolbar

Instead of defaulting, you set it yourself; so it's easy to think of an error when you need to change the theme of MainActivity to the above. Open the AndroidManifest.xml file and modify the theme of the corresponding Activity to @ style/Theme.AppCompat.Light.NoActionBar.

Without the effect of bar

Open the layout file under the corresponding Activity. Here is MainActivity, and the layout file is activity_layout.xml. Add to the androidx.fragment.app.FragmentContainerView:

Let's see the effect first:

Set the effect of the head yourself

Next, let it display the content.

Also added in Activity:

@ Overrideprotected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager (). FindFragmentById (R.id.nav_host_fragment); NavController navController = navHostFragment.getNavController (); AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder (navController.getGraph ()). Build (); Toolbar toolbar = findViewById (R.id.toolbar); NavigationUI.setupWithNavController (toolbar, navController, appBarConfiguration);}

In this way, Navigation will automatically host Toolbar, so that you can easily achieve the above effect, and the rest of the code does not need to see the effect:

The final effect of using Toolbar

This is the end of the article on "how JetPack uses the Navigation menu in Activity". Thank you for reading! I believe that everyone has a certain understanding of "how JetPack uses the navigation menu in Activity". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report