Issue with Camera in fragment - java

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.

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

Android: How to open a new full screen fragment from a fragment

I would like to open a new full screen view (layout) from a fragment without have to create a new activity.
I know how to do it creating a new activity but I read you better "always" use multiple fragments, not multiple activities for your full project so I am following that "rule"
You can check the images attached to explain the situation I would like to reproduce. As you can see, from a fragment with a listview, it open a new full screen view. Tapping the back button in the action back, you go back to the main with navigation, toolbar and the list.
I am using xamarin but a suggestion using java will work too for me. I just need to understand the concept of how to do this and what kind of controls do they used.
Thanks in advance.

swiping down a fragment and taking a view to bottom|right on any other fragment like Youtube player does? (draggable panel deprecated)

I need exactly same work like youtube does. the way it minimizes the whole fragment and just keeps the player view ON, on any other fragment or activity.please provide a better resolution. The draggable panel is deprecated.
You can implement this on your own using ViewDragHelper. Check out this link to see a sample of ViewDragHelper http://fedepaol.github.io/blog/2014/09/01/dragging-with-viewdraghelper/

How to do a "swipe information" activity?

In my project I have an Activity that loads a list of news. When the list is loaded, this Activity is populated with the information of the first item on the list:
But since this loads all the news from the server, I want the user to be able to swap the news with his finger. For example: we start with news[0] and user swipe left, the content change to news[1]. The user swipe left again and the content changes to news[2] and so on... if the user swipes right, then it return to the previous news (news[1]). The header should not have a swipe movement, only the main frame (image and body should change):
What would be the best aproach for this behavior? I was thinking about using a TabLayout with ViewPager but I'm not sure if this is the correct way to do this. If I have something like 100 news this could be a overkill to load 100 fragments in the ViewPager right?
Does android have a better way to do this? How should I do this?
You can use ViewPager for that. Having 100 fragments inside is totally fine because ViewPager will only load as much as you set using setOffscreenPageLimit(pageLimit). For instance, if you set pageLimit = 3, ViewPager will only initialize 3 (up to 6) neighbour fragments that are on the right/left side of your currently visible fragment. And while you're swiping through fragments, it will kill the fragments that are out of this limit, and load new ones. So, there will be no overkill.

Categories