Android - Adding VideoView to a fragment - android

I'm experiencing a strange problem. I have an app, based on fragments. When I want to change fragments, I first check if the fragment is added. After hiding the previous fragment, if the next fragment is already added before, I just show it. If it is not added before, then I add it. And one of my fragments has a VideoView declared in it's XML file.
Now my problem is, if the fragment is not added already, when I try to add it, the whole screen goes black for a second and then comes back with the new fragment. The portion that I am placing the fragment is only a part of the screen, so it should only refresh that portion of the screen. After the fragment is added, when I try to navigate to it (when I use show() method), I do not experience this behaviour. I believe this has something to do with the VideoView, because if I remove it from XML, I no longer experience this problem. I tried adding the VideoView programmatically, but I still had the problem. I also tried to use a SurfaceView to play the video, but in that case, again I have the same problem.
I would appreciate any suggestions. Thank you.

Related

Android: After orientation change breaks connection between fragments

Application has two fragments: the first one contains a small representation of pager with photos, and the second one contains full screen pager. The second fragment replaces the first and passes a page number to the previous every time it changes. I made connection between my fragments just like Android Developers says.
Everything works till device orientation doesn't change. The first fragment is not recreated until it is not on top of stack, that is why all page number changes after that are missed for first fragment.
I do not really what to disable views destroy on orientation change, but looks like it is the only way.
What is the best solution?
In your manifest file in your parent activity in which fragments are write following line :
android:configChanges="keyboardHidden|screenSize|orientation"
Let me know if that works for you. Best of luck :)
When you change the orientation, the activity gets rebuild, thus essentially destroying the fragment that was built before the change in orientation.
Perhaps you would wish to refer to this https://developer.android.com/guide/topics/resources/runtime-changes.html on retaining the state during a change in configuration.

Android Studio multiple fragments

I'm designing a music player app and this is my second time working with android studio and fragments in general. I've watched a lot of tutorial videos on this but I'm having trouble getting multiple fragments to show. Maybe using fragments isn't the way of going about this. But basically what I have right now is a fixed menu at the top and 5 fragments just below it (on top of each other).
What I was hoping to do was by clicking each button one fragment would load so you would have one activity but 5 fragments that load when buttons are pressed. But the issue i'm having right now is instead of stopping one fragment and loading another one, they're being loaded on top of each other. Is there a way to go about doing this?
Below is an image of the design I had in mind. The blue box is where the five fragments are and right now they're overlapping. Any help would be appreciated :)
EDIT: Turns out the answer was I needed to define one layout for all of the fragments. Thanks everyone.
Please post code. But here's what I can make out so far from your problem,
I assume you are just using FragmentManager.beginTransaction.add() again and again.
If it is the case please change your code to
FragmentManager.beginTransaction().replace(
containerViewID,
new FragmentYouWantToReplace
);
What this does is removes previous fragment and adds this new fragment.
So you can have your default fragment using add() during activity load, but for each button click you use replace().
When you call a new fragment you should replace always the same layout, the one you want to show the information, like this:
frag_results frag_results = new frag_results();
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.framelayout_secundary, frag_results, frag_results.getTag()).commit();

Implement hide()/show() instead of using backStack with Android Fratments

So, I'm still new on Android Fragments, but if I got it correctly, when you remove a Fragment and stack it to the back stack with addBackStack (according to Android Developer's Fragment lifecycle) you get this portion of view destroyed and when it pops up to the screen again it has to reload all it's components.
I'm specially concerned with that because one of my fragments contains a map, which is a lot of data to reload (specially if using 3G).
In that case, is it a better practice to implement hide and show to all my fragments when manipulating them?
I just see a little problem there because I'd have to create my own stack to know the order they were called and have to be shown again.
Well, it's now a huge problem, but I wanted to hear from someone if it is worth putting effort on it to implement this approach instead of the existing back stack.
Thanks in advance!
Using the ´attach´ and ´detach´ operations are another way around to your problem. The fragment instances will still exist, but deactivated and not visible, and you can recover them using the FragmentManager
If one of your fragments contain google map, it's better to use show/hide operations with the fragment views in order to avoid google map black flickering bug. Check my answer: https://stackoverflow.com/a/25206078/2999943

Android: backbutton animation when orientation is changed

I have a rather weird problem. Everything is working as it should except for this little thing. I'll show the code.
// ...lots of other code
ft.setCustomAnimations(R.animator.slide_in_right, R.animator.slide_out_left, R.animator.slide_in_right, R.animator.slide_out_left);
ft.addToBackStack(null);
// ... some more code
It's pretty simple, when I click a button that replaces a fragment with another fragment an animation between the two is played out. This works dandy! And as you see, the same animation is presented when I press the back button.
It all works! Until... I change orientation. Let's say I'm at the third fragment, change orientation and press the back button, the animation does not play out. The previous fragment is displayed instantly. When I click a menu button again to the third fragment the animations all of a sudden start to work again.
I'm guessing this has something to do with the Bundle, as when you change orientation the Activity is stopped, then started and the savedInstance being "reinserted". However the savedInstance animation was made for portrait and not landscape. Or it's simply just not saving the setCustomAnimations().
Is this a bug or is this how it is supposed to be?
As far as I know, this is a reported bug both in the native and support libraries.
Look at this question, or here for some advices. I tried the last advice in the second link and it works fine now.

Getting blank screen when switching between activities

I am working on an app , where I am switching between two activities. When control is inside onStart of second activity , there is screen drawing and processing logic being handled.Because of this , when switching between the two activities happens, there is a blank screen that comes up.
Along with this , i also need to render a live video feed in background of my activities/app
What could be the best way to deal with this?
TIA
If you're getting a blank screen it could be because your activity is setting a new contentview but it hasn't been processed properly. Does it ever load completely? If it's just that it's black at the beginning and then renders. Try setting the contentview at the end of your switching in onCreate. This will make it remain on the first activity and avoiding switching views on till everything is loaded.
With regards your second question. Is the live video feed from the Camera?
If it is, set the camera view in the background, make a transparent LinearLayout (or whatever your layout is) over it and then put your various views there.
Your question doesn't have enough specific information to get an accurate idea as to what you're talking about, if my answer doesn't address your problem please respond with elaboration and some code! :-)

Categories

Resources