Recently, I am working on a small project, which mainly uses Navigation in Android Jetpack to realize single Activity architecture. Although there are many articles introducing Navigation on the Internet, there is a lack of actual development instructions. In this development, I encountered a lot of difficulties because it was the first time that I adopted the single Activity method. This article records some problems of using Navigation to realize the single Activity. Avoid the same problems and delay time.Copy the code

The use of Navigation will not be introduced here, there are a lot of online, beginners of friends to check by themselves. Only problems encountered during development are recorded here. The basic problem is that every time the fragment is rebuilt, the page is refreshed every time. The solution is to save the created view and determine whether the view is empty every time onCreateView is created. Let’s take a look at the other problems

The Fragment cannot be defined in XML when the Fragment is being written. This will cause an error. You can only write a layout in XML as before, and then add fragments dynamically.

Define a local fragment variable in your Activity and point that fragment at yourself every time your Base onResume, like this

You then define a method in the BaseFragment that subclasses override and return true if they need to handle the Back event

Finally, specific events are handled in the Activity

Problem 3: Handling the Back event of exiting the application; It’s easier to deal with. First we go to the first layer of our Fragment, which is the closest Activity to our Activity. I named it “MainFragment”. Then we override the method we defined in the BaseFragment step to intercept and return true

The code will not be translated for you, at a glance, OK problem three solved.

Problem 4: Jump to the same page. There is a requirement like this, take the article for example, in the article details page, watch the article, para para is full of text and text mixed content, and then there is a list of recommended articles, click still enter the article details page. At first, I used the current fragment to point to myself in the edit page of navigation, but found that the jump could not be completed at all. Solution steps:

In the NAVIGATION XML, Add the fragment, right-click Add Action and select Global

So it will have an arrow to the left pointing at it:

Switch to the text

You can see that the system generates an action for us that points to the fragment we just added. Then use the action where you want to jump.

Problem 5: A goes to B, and B goes to C again. After C finishes the operation, it wants to go back to A directly. The default is not to press the return key once, we need to call:

Navigation.findNavController(view!!) PopBackStack (R.i d.m ainFragment, true), Public Boolean popBackStack(@idres int destinationId, Boolean inclusive) this backback method takes two arguments, the first: destinationId, Note that the id defined in the fragment navigation is not the actionID. The second parameter is whether to remove the destination from the stack. In this jump process, A->B->C, there are A, B and C in the stack, so we need to remove the target from the stack when returning A. And all fragments in the process are removed from the stack.

Problem 6: Open screen page, which is the most difficult to solve, is the solution I can bypass or fundamentally not solve. DefaultNavHost = “true” because when navigation is configured, we need to set a root target, which is the first fragment entered by default after startup. This will end up in the startDestination of this configuration by default every time we return. So we can’t make our SplashFragment do startDestination, which would make the Splash page the root. Oneself also tried a lot of means, used the method that does not calculate method finally… That is, everything is normally configured. Only after MainFragment is enabled, it immediately jumps to Splash and then returns to Splash. Of course, popBackStack is also used to return to Splash.

This article records some practical development problems encountered in the author’s attempt to implement single Activity architecture with Navigation. At the beginning of the article, it also says that there are many online articles about Navigation, but most of them are just some introductory usage, and few information can be found on some practical problems in the actual development. If there is insufficient hope we leave a message to discuss, especially for the article in question six to supplement. Deficiencies please correct, do not spray light spray, thank you.