I'm using this tab navigation: https://developer.android.com/samples/SlidingTabsColors/index.html
From the Android Developers site.
However I am unable to use a camera in any simple way, as the onActivityResult is never called. Apparently it works fine for fragments but presumably because the SlidingTabsColors example uses nested fragments (I think?) with the SlidingTabsColorsFragment creating (?) new fragments like this:
Fragment createFragment() {
return ContentFragment.newInstance(mTitle, mIndicatorColor, mDividerColor);
}
So my onActivityResult is never called. I have tried every variation of calling it that I could find suggested.
I've got some incredibly strung out code in the initial activity now that looks for the fragment and passes it the requestCode, resultCode and the data, all of which are off or null though (possibly another problem).
Is there a way I can have a smooth, tabbed navigation and be able to use the camera? I've not included any specific code as it's just the base code I mentioned, with a variety of attempts at simply calling a camera and saving an image. The camera opens and works fine, we just never get to onActivityResult. Is there perhaps another way to go about this?
Thanks for any help.
Related
While peer reviewing a colleague's code I noticed she created a new Activity and all functionality is just there without a Fragment.
In the old days of Android, this is what we did, but the last few years I and my peers always took the approach that every Activity should have at least one Fragment and no actual code should be written in the Activity apart from loading the Fragment of-course and maybe some higher end procedures.
I want to argue for always using at least one Fragment in every Activity, but I couldn't find compelling arguments about why it is better than a no-fragment Activity.
The out of the box argument I can think of is that it will be easier adding new fragments if needed, but if we know this will never be a necessity, why bother with a single fragment Activity?
Fragment is easier to extend and test, if you are writing another new feature, it is helpful for separating code. And you can also move your fragment code to another place easy.
Of cource, if you are sure that your code is very simple and stable, like demo or temp test code, you can also use Activity without fragment.
Since the new upgrade I have no idea on how to navigate with fragments.
A lot of code from here is commented out. If I use the code that is left, it doesn't work. I get the activity not found exception. I use the same headers as written in demo example. What are the steps I need to follow to create a succesful fragment?
The new fragments are actually pretty straight forward. All you have to do is annotating your fragment classes with the MvxFragmentAttribute and then making sure that the type passed as the first argument of the MvxFragmentAttribute implements IMvxFragmentHost, so that it can handle the Open and Close methods used by the MvxFragmentsPresenter.
The new MvxFragmentsPresenter will handle whether or not the current top Activity is responsible for showing the requested fragment or not and, if it isn't, it'll start the proper activity in before showing the fragment.
I'm using the viewpagerindicator library (http://viewpagerindicator.com/) to create some sort of wizard for my android app. It works fine and does exactly what I want.
I would like to "extend" the functionality a bit by having "previous"/"next" buttons in my ActionBar - pretty much as in Android's "Done Bar" tutorial - to step through the wizard. Works like a charm, too.
HOWEVER:
I would like to display information about the "next" & "previous" fragment in the ActionBar's buttons. Information I pass to the fragments that live in the ViewPager at the time of their "creation" (actually at the time of their object instantiation - using the classical "newInstance(...)" approach to create the instance of my fragment, store the parameters in a Bundle and extract them in the fragment's "onCreate" method). The same way the template does it, when you create a new fragment for your project.
So, this information is the thing I actually want to display in my wizards button to know what fragment is next and which was last.
The type of this information is not important for my problem. It could be a String or an icon or an int or ... anything else you want.
However, wherever I've tried to access my fragments data, the fragment has not yet been fully initialized (meaning its "onCreate" method has not been called yet).
I've tried it in the host fragment's "onViewCreated" method, because I thought that's where all its subviews should be initialized already (at least their "onCreate" method should have been called, I thought), but it seems that this is handled differently for ViewPager to retain only the number of fragments in memory that was set by setOffscreenPageLimit.
So, what I'm looking for (and probably just missing) is the correct callback method here. One that is called when the ViewPager's next Fragments have been loaded and initialized. If such a callback exists, I could place my little piece of code there to update the text in my "previous"/"next" buttons within the ActionBar.
Any help, comments, ideas are highly appreciated. If needed, I can also try to attach some code sample to better explain my setup, but I think it should be easy enough to understand what my problem is.
Thanks in advance!
P.S.: I also tried to do this by using EventBus to send "onFragmentInitialized" messages from my fragments within in the ViewPager and the hosting fragment. It actually worked, but it does not seems the proper way to do this.
When a Fragment's onCreate Method is called, its already preparing to be displayed, and practically its past the point where its considered a Next or Previous fragment instead its considered current.
A fragment's onCreateViews method is called after committing a transaction in the FragmentManager. which takes less than 1 sec to bring it in front of the user (depending on the device and runtime environment)
But in your case, your data should be initalized outside the Fragment that uses it, and displayed where ever you want by passing the data itself then displaying whatever you want form it.
decouple your data from android objects (Fragment, Activity ...) and you should be able to load, maintain, access it cleanly and without worrying about their callbacks.
The Fragment's arguments can be read and loaded in its onAttach callback rather than onCreate, the Activity will then (after onAttach is complete) get a onAttachFragment callback with the Fragment as a parameter. However, I doubt onAttachFragment will be called when switching between already loaded pages in the view pager.
If not, you could have the fragment notify the activity (through an interface) that it is now active during its onActivityCreated, onViewCreated or similar method.
But it sounds more like the activity should register as a page changed listener to the ViewPager itself, and update its state depending on the page rather than which fragment is active.
As a side note, ViewPagerIndicator is quite old now (hasn't been updated in 3 years), a more modern approach is the SlidingTabs example from Google, which has been built into a library available here: https://github.com/nispok/slidingtabs
I've read many posts on stackoverflow about fragments vs activities, but I'm not sure I understand. Am posting a problem I'm working on -- hopefully some of you could help me clarify what they mean in this context.
I want to build an app with two tabs: "take photo" and "browse photo". In take photo, the user can take a photo. In browse photo, the user can browse photos already taken.
So I've made two tabs so far in MainActivity, which extends FragmentActivity and implements ActionBar.TabListener. onCreate of MainActivity creates a SectionsPagerAdapter, which extends from FragmentPagerAdapter. The main purpose of my SectionsPagerAdapter is to create new Fragments. It creates a TakePhotoFragment and a BrowsePhotosFragment.
Question: in TakePhotoFragment, should I create a new activity that takes the photo? I do know how to create an activity that allows the user to take a photo, but not sure if this is right in this case. I did read that fragments are primarily for UI reasons and sit within activities, so it seems kind of weird to create an activity in a fragment (and also I don't know how this could be done).
I guess the main problem I'm facing is that I'm really confused about how activities and fragments can be used, despite all the reading I've done about them. Perhaps someone can help elucidate in the context of what I'm trying to do?
Thanks!
Well just some clarification, a fragment is a content that be put into an activity, not an activity. You will always have one Activity which can host fragments but not the other way.
For the camera thing, it's like using any other feature, you will call an external activity or service, you can do that inside the fragment, just like voice recognition or barcode scan.
Here is a simple example you can try, and adapt it for a fragment.
http://developer.android.com/training/camera/photobasics.html
Hope it helps, good luck with your research .
First of all, you CAN'T create an Activity inside a Fragment.
If you have a code which works within an activity it should also work within a fragment. Just use the fragment's onCreateView instead of Activity's onCreate.
If you use some kind of SurfaceView for taking photos it works within any context. If you call an outer camera intent it has nothing to do with your layout, it just calls the device's photo app, so you can't put it inside your fragment.
I'm having a problem instantiating Fragments in my program using the Support Library implementation. Here's a brief description of the task I'm attempting to perform and some of my attempts which haven't yet borne fruit:
The UI of my application is subject to change to meet user preferences. In order to do this, I'm using a Fragment for each different layout and replacing the active Fragment in the UI at a given time as per the user's instructions. Here are some ways I've tried (and failed) to do this:
I've tried adding the Fragments as non-static inner classes in my Activity. This approach worked so long as the user did not rotate the device. As soon as the user rotated the device, the application crashed (this is true for Portrait -> Landscape rotation and for Landscape -> Portrait rotation). Upon checking the issue using the emulator, I was getting an InstantiationException. I checked SO for some help, which led me to:
Implement the Fragment as a static inner class. When the Fragment initiates, it will expand its layout, and then from later in the control flow of the Activity, I can do stuff to the Fragment's subviews (in particular, add listeners to the buttons). Unfortunately this didn't work because I couldn't refer to the Fragment's subviews using [frag_name].getView().findViewById(). Something about referencing static objects in a non-static context. Once again, I checked SO, which led me to:
Implement the Fragment as a separate class altogether from the Activity. This seems to be what the Dev docs on developer.android.com recommend. Upon doing this, everything seems to compile fine, but when I try to refer to the Fragment's subviews (once again, using [frag_name].getView().findViewById()), I get a NullPointerException. When I add System.out.println() statements across my code to find out exactly what is happening, I find that the print statement inside onCreateView in the fragment is never getting fired, which implies that onCreateView is never getting triggered.
So now, I'm stuck. What am I doing wrong? The precise implementation of this isn't as important as learning something from the experience so I can get better at Android development, so if seperate classes are better than static classes or vice-versa, I don't really care which I use.
Thanks.
Figured it out. Turns out that in order to do what I wanted, I had to register the Activity as a Listener to each of the Fragments and pass "ready to enable buttons" messages back and forth between the two. To anyone using this question for further research, the guide on how to do that is located on the Android Developer guide, here: http://developer.android.com/training/basics/fragments/communicating.html