Fragment that covers bottom navigation and loads smoothly - java

My app uses a Bottom Navigation, which is tied to a NavController. I want to load a fragment that creates a new item in my app, so I want the fragment to take up the part of the screen used by the bottom navigation. I am trying to make my app consistent with Google's, so I am using a fast entry animation from the bottom.
However, by default, the NavController loads in the NavHostFragment which is above the BottomNavigationView, so the Bottom Navigation is shown with any fragment. I tried using the Google's suggested method to Listen for navigation events. However this sets the BottomNavigationView to gine before the fragment navigation takes places, shifting the current fragment downward, giving a stutter effect on navigation.
What makes things worse is that it seems with just about any complexity of the target fragment, it takes a moment to inflate the layout of the new fragment, so the BottomNavigationView disappears, the app pauses for a moment while inflating, and then it is too late for the animation and the fragment stutters into existence.
What is the right approach for this?

Try setting the exit transition of the every those Fragments from which another fragment(which creates new item) is loaded like this:
setExitTransition(new Hold());
Basically, this holds the current fragment until next one is loaded.
Note: If you do not find Hold class in your current implementation, you might need to use the alpha version of MaterialComponents.
implementation 'com.google.android.material:material:1.3.0-alpha01'

Related

I don't understand how to organise my fragments

I have realised an NFC reader application
So i have 3 activities :
MainActivity, which is an activity who contains a Button. If button is clicked, the scan is activated and the user can put his NFC tag against the device to detect it.
WebActivity, who is launched if the NFC tag contains and URL (and open a WebView) or if the user want to launch WebActivity by himself
HistoryActivity, who gonna contains a list of every scans.
Now, I would like to swipe activity with a finger gesture. according to my research on Internet. I need fragments and ViewPager.
But every example that I saw is bases on ONE activity and multiple fragments.
But in my case, I have to create 3 fragments (one per activity), right ?
And I really don't know how to manage my fragment. I mean, what to put inside ?
All I want to do is create a transition/animation while changing activity... That's crazy
This is too broad of a question but hopefully my answer will steer you in right direction.
You should definitely go with single activity/multiple fragments model. Aside of recommendations by Google, you could use navigation components, deep linking much easier then without single activity.
Yes you should be using ViewPager for the purpose (and likely your implementation of FragmentPagerAdapter as well) however I do not understand what kind of swiping will you be doing
Reading your setup, I would suggest to use bottom view with 2 items (good example is here https://github.com/android/architecture-components-samples/tree/master/NavigationAdvancedSample/app/src/main/java/com/example/android/navigationadvancedsample). 2nd one would show history, first one would offer a button that activates your action, and then displays fragment with your WebView.
As a side effect of such implementation, you'd be able to go back from 2nd bottom view item to whatever first one holds - by pressing system back button - which I think is nice touch.
UPDATE to "swiping takes place anytime. " comment:
You could have single activity, ViewPager with 2 fragments. First fragment would display a button, 2nd fragment would display a history. You could freely swipe between them, as you want to. However to me it does not make sense to put WebView screen into this. WebView screen is result of action (NFC detection) and it should probably display as full screen, without any chance of swiping between main/history and itself. Hope it helps or I'm missing some important piece of info you did not share.

What are the general instructions to add a new fragment that uses a new screen and has its own layout?

I'm struggling to figure out how to create fragments that have their own layout files and take up the whole screen, as opposed to adding them to the activity's layout.
For instance, in my activity there is a button which should call a RecyclerView Fragment that takes up the whole screen, let the user pick an item, and then return to the activity. All the examples I'm finding though use transactions to add or replace on the activity's layout. How do I make fragments that are inflated from their own layout files and call them from the activity?
And sorry, I'm sure there's a better way to ask but I'm just going through docs and vids trying to learn.
A few line difference between Fragment and Activity:
An Activity is an application component that provides a screen, with which users can interact in order to do something. More details: http://developer.android.com/guide/components/activities.html
Whereas a Fragment represents a behavior or a portion of user interface in an Activity. http://developer.android.com/guide/components/fragments.html

How should I structure bottom navigation in Android coming from iOS background?

Some background:
Coming from an iOS background, using UITabbarController is very common and straight forward. Each Tab in the tab controller will change the current view to another UIViewController, and each of these UIViewControllers can have its own NavigationController (which kind of acts as a back stack). So whenever I switch tab, I would resume to the state where I left off.
Now I want to implement the same thing in Android, but it seems like the use of ViewController is different in Android. After digging around, I read that instead of using Activity like UIViewController, I should use Activity to act more like NavigationController, and use Fragments (which is deprecated)
to act as UIViewController instead.
However my question is:
Should I be implementing multiple Activities for Bottom Navigation? When I click on each item in the Bottom Navigation should I use an Intent to change Activity? Because from my understanding, using Intent to change Activity will add the new Activity to an Activity back stack, which would prevent me from switching back to whichever Activity I want. If someone could, Please tell me what is the "right" way (if there is one) to structure Bottom Navigation. Thank you all in advance.
You can use fragments as UIs, And Use a BottomNavigationView in your activity or you can use some libraries.
Here is a library for better customization: https://github.com/ittianyu/BottomNavigationViewEx
Native Method:
https://medium.com/#hitherejoe/exploring-the-android-design-support-library-bottom-navigation-drawer-548de699e8e0
In Android you should use Viewpager, tablayout and Fragments. Just search for its tutorials. there are lots of them on internet

Issue with Camera in fragment

I have Activity which contains ViewPager which implements FragmentPagerAdapter with 3 fragments and I have implemented Camera in one of the fragments using SurfaceView. So when users swipes to the left from my home fragment he is able to use Camera (like in WhatsApp). The issue is when starting app (where I get some data across internet and put into listview) or sometimes swipping to the left or right Camera appears in main or home fragment for a second or less and then disappers and home fragment is presented at it should be.
Here is the problem when downloading data, camera occupies home fragment until
data is loaded. And for now that is for a second or less:
https://i.stack.imgur.com/wsEUy.jpg
And this is how home fragment actually looks after downloading data:
https://i.stack.imgur.com/AUv3V.png
I tried with adding background color to my home Fragment container xml
but that didn't work.
I followed this example to implement camer in fragment:
https://github.com/rexstjohn/UltimateAndroidCameraGuide/blob/master/camera/src/main/java/com/ultimate/camera/fragments/NativeCameraFragment.java
Can someone help or give some suggestions what's happening and what to do? Thanks
I found solution which fits my needs. So here it is if someone in future stuck with same issue. Somehow the problem was when I add my CamerPreview to View in my case that was FrameLayout and that was in OnCreateView of CameraFragment. Even if I try to start camera preview when CameraFragment is visible to user and stop preview when it's not visible that didn't help I was always getting flash over home fragment by Camera preview when swipping to the left or right.
So I create new Fragment which is like placeholder and I added it to FragmentPagerAdapter and then there was an option to create my CameraFragment using FragmentTransaction when my placeholder fragment is visible to user (that works too) but I used ViewPager detecting when user starts swipping to the left then FragmentTransaction begins to replace my placeholder fragment with actuall CameraFragment.
Transaction isn't quite somooth like for example in WhatsApp (If someone knows how they done that and wants to share, that would be great) but it's working without issues and no flashing over home fragment.
Of course if someone have and idea how to improve solution or some another better solution feel free to share it.

Keep d-pad focus changes within current fragment

I have an android app that primary receives input using a d-pad (up, down, left, right, OK, back)
The app runs on a TV so uses fragments to separate sections.
I would like to keep directional buttons from focusing views in other fragments. And handle directional buttons differently depending on what fragment is set as the "current"
What would be the most elegant solution to this?
also, Fragments are dynamically changed at runtime.
When the d-pad is the only means of navigation for a user, such as on some Google TV's, then it's not a good idea to stop the navigation at the edge of a fragment. How I solved this, is by allowing cross-border navigation and laying out elements so that the navigation is not awkward and works by finding the path automatically (after all, there does not seem to be a way to statically provide the next element to focus on in each direction, when in another fragment).
Like you I'm looking for reference advice on how to deal with fragments and navigation on Google TV.

Categories