Fragment Layout Name - android

I have been using Android ADT for a few weeks now and i used to normally create new activities by going to:File>New>Other>Android>Android_Activity>Blank_Activity. However after updating my 'Android SDK Tools' & 'Android SDK Platform Tools' today , its now showing a new option when I'm trying to create an activity.
When I select new blank activity and click next , its then showing a new form that i have to fill in which is titled as 'Fragment Layout Name'. Why has this suddenly appeared and does anyone know why I'm being forced to create a fragment layout as i don't want to even use this. I also remember one of my friends saying that he updated his SDK about a week ago and he stated that he had the same problem. Shall i just remove the fragment in the XML document once its loaded or is there a way to disable this so i wont have to go through this every-time.

The templates of files produced by "New ..." wizard have changed lately. Now you are enforced to create a new Activity with a Fragment placeholder attached. Guess that's the way the developers enforce people to build UI based on Fragments :)
There are a few workarounds for your problem:
1) Just delete fragment layout in your resources; delete the placeholder fragment in your Activity code and all the relevant code (FragmentManager etc); change activity layout from FrameLayout to anything you like.
2) Follow the recipe proposed in this answer
3) Don't. Ever. Use. Wizards. It is much better to create a new class and write extends Activity (or Fragment, Service, etc), than to create a template and waste your precious time changing it to your needs. And it helps to understand the lifecycle of components, too.

Related

Using a no-fragment activity instead of a one-fragment activity

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.

How to call a new activity without calling a new "window"

I am trying to call a new activity using an intent, but every time I call it, I can see a new "window" open on my android device. Can I call a new activity that will be in the same window? What I mean is calling new activity without visually seeing that it has been opened.
Hope you understand my question :) Thank you!
I think what you are looking for is Fragments
Start reading Here
Fragments actually use the "same" window (Activity) and just replaces layouts and views - I think exactly like you want.
If you want to run something like a unix-demon code(this mean a program that is only executed in the background without no visual components) you are looking for a android Service.
http://developer.android.com/guide/components/services.html
Otherwise if you are looking for a visual component that don't create new windows but refresh the old one, you could use the Fragment class if your android version is 3.0 or higher.
http://developer.android.com/guide/components/fragments.html
In an Activity content view, create an empty relativeLayout.
As mentioned in previous answers, create fragments and replace this relativeLayout with the newly created fragment using FragmentManager.
FOr reference use this.
Its really easy. try this.

Android SDK creates FragmentActivity instead of Activity

I came across this article but this article suggests replacing the FragmentActivity's code with that of the old Activity code. I can do that but would like to know if there's a way to just create Activity instead. I updated to ADT v22.6 today. Could that be the problem?
Another problem is that this requires a min-SDK of 7 while right now I want a min-SDK of 3.
Also, I am using Eclipse and not Android Studio.
Thanks in advance!
The new ADT 22.6 has new feature added that it will automatically create the any new activity as FragmentActivity not an single activity. So if you wish to create an Activity you can create it by explicitly creating separate class and extends Activity.
I'm afraid that google is enforcing us to use fragments activities instead of plain activities now. You either can go back to an SDK previous to 22.6 or just create the activities by just extending the class, adding a layout and adding the activity reference to the manifest.

Eclipse Android Custom Activity Class File (subbing for autogen activity file for new project)

When I create a "New Android Application Project," I would like Eclipse to proceed with its normal actions with (at least) one customization.
I want Eclipse to create the new Activity.Class file using fuller #Overrides rather than just the onCreate(), such as also providing onStart(), onResume(), onPause(), onRestart(), and so on.
In other words, I want a more verbose Activity.Class file generated than is already done by Eclipse.
Going further, I would also like Eclipse to add my own inner classes - if that is not asking too much.
In short, is there a template in Eclipse that I can tailor for this customization?
I am not aware of a way to accomplish this within the "New Android Application Project Dialog", but you add additional overrides to each activity yourself by clicking the Source dropdown menu, then Override/Implement methods..., and then select each method you would like to override.
I'm not sure what you mean by add your own inner classes. You can do this easily yourself, I'm not sure what additional functionality Eclipse could provide.
This might answer your question. There's a templates directory in your Android SDK where you can add or alter the XML templates.
Good luck,
Bp

Fragment not instantiating correctly

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

Categories

Resources