I am following the example here
http://developer.android.com/reference/android/app/TabActivity.html
creating tabs using fragments and in the tutorial it uses TabManager but on my version it says it cannot be resolved.
I am using minsdk set to 14 and targetsdk set to 15.
I don't wish to use the backward compatible support.
Any ideas which package it is in?
Eclipse doesn't provide me any help, only asks me to create the class TabManager.
Thanks in advance
TabManager is for the compatibility fragment tabs, not for the 3.0+ fragment tabs. That's why it can't be resolved.
The tutorial here shows how to create fragment tabs in 3.0+:
http://developer.android.com/reference/android/app/ActionBar.html#newTab()
Also, you don't have to inherit from FragmentActivity starting in Honeycomb unless you want backwards compatibility. You can just inherit from Activity and Fragment will be supported.
Related
I am trying to use getChildFragmentManager() method on api 14, but of course I am getting error. Is there anyway to use this method on lower apis.
Thanks
To use nested fragments on API Level 16 and below, you need to use the fragments backport from the support-v4 or support-v13 portion of the Android Support package. This, in turn, requires you to inherit from FragmentActivity and have your fragments inherit from android.support.v4.app.Fragment. Then, you can call getChildFragmentManager() to use nested fragments.
To someone that appeared here by a search on Google
I was having a similar problem using DialogFragment
The problem was because I was importing android.app.DialogFragment instead of android.support.v4.app.DialogFragment
DialogFragment or not, be sure that you are importing the right libs :)
Now as default AppCompatActivity is extended by default by Android Studio.
But if I am going to use more of fragments, then is it wise to use Fragment Activity or Activity instead of AppCompatActivity ?
On Developer.android site, they mentioned this :
↳ android.app.Activity
↳ android.support.v4.app.FragmentActivity
↳ android.support.v7.app.AppCompatActivity
Activity is our simple Activity without action bar.
Fragment is our simple Fragment and FragmentActivity if you want to use getSupportFragmentManager()
ActionbarAcvity if you want to use action bar in activity which is deprecated in 5.0. To support action bar in 5.0 and prior use AppCompatActivity.
Hope it helps.
The answer for all modern app development, since Android 5.0 Lollipop came out and introduced the appcompat library, is to extend AppCompatActivity and use support fragments (getSupportFragmentManager instead of getFragmentManager.)
There's no good reason not to; the support and appcompat libraries have caught up to the framework in every feature and boast much wider compatibility.
In Android what is the main difference between extending Lifecycler Activity, Activity,ActionBarActivity & AppCompactActivity? How do these classes differ from each other in terms of usage?
extending ActionBarActivity gives you the ActionBars functionality on every API level >= 7
by extending Activity you can avoid adding additional projects/libraries to your project but you'll lack the ActionBar on api levels below 11
edit: More details:
ActionBarActivity is part of the Support Library. Support libraries are used to deliver newer features on older platforms. For example the ActionBar was introduced in API 11 and is part of the Activity by default (depending on the theme actually). In contrast there is no ActionBar on the older platforms. So the support library adds a child class of Activity (ActionBarActivity) that provides the ActionBar's functionality and ui
edit2: Update April 2015 - it looks like the ActionBarActivityis deprecated in revision 22.1.0 of the Support Library. AppCompatActivity should be used instead.
edit3: Update Aug 2017 - LifecycleActivity is a LifecycleOwner but:
"Since the Architecture Components are in alpha stage, Fragment and
AppCompatActivity classes cannot implement it (because we cannot add a
dependency from a stable component to an unstable API). Until
Lifecycle is stable, LifecycleActivity and LifecycleFragment classes
are provided for convenience. After the Lifecycles project is
released, support library fragments and activities will implement the
LifecycleOwner interface; LifecycleActivity and LifecycleFragment will
be deprecated at that time."
(copied from the Architecture Components guideline)
If you look carefully, you will see this
public class ActionBarActivity extends FragmentActivity
implements ActionBarDrawerToggle.DelegateProvider TaskStackBuilder.SupportParentable
Here you can read about FragmentActivity:
http://developer.android.com/reference/android/support/v4/app/FragmentActivity.html
And differences between Activity and FragmentActivity:
Difference between Activity and FragmentActivity
Also, there are some new themes for styling actionBar...
https://developer.android.com/training/basics/actionbar/styling.html
Actionbar is introduced in API level 11. com.android.support:appcompat-v7:+ is a support library which allows you to have an ActionBar in your app for devices running on Android 3.0 or below. So, if you need actionbar below api level 11 your Activity needs to extend ActionBarActivity.
If you are targetting api level 11 and above then you don't need to extend ActionBarActivity and reference AppCompat. You can simply extend Activity and you will have actionabr by default.
Android Studio default project includes it automatically in dependencies and extends ActionbarActivity instead of Activity in order to use it.
The ActionBarActivity or the SupportActionBarActivity have additional methods and properties that are not in a generic Activity. for example methods for adding tabs are present in the ActionBarActivity and not in a generic Activity.
The major difference being you don't get an ActionBar on a generic Activity.
ActionBarActivity just has more support libraries and better usage of the newer themes available from api 11.
"In its most basic form, the action bar displays the title for the activity and the app icon on the left. Even in this simple form, the action bar is useful for all activities to inform users about where they are and to maintain a consistent identity for your app."
You are using Android support library When you come to to the Actionbaractivity . so the uses of Support library is your application can be suport for maximum number of devices. Support library gives to your application the power of Backward compatibilty. Actionbaractivity gives you the mulitiple feature like Actionbardrawer toggle etc.. there are more support libraries available. see this link .. and share to your friends...https://developer.android.com/tools/support-library/index.html
I am looking at fragments and its usage in Android. I am however getting confused as how do fragments work for all the different Android versions. What i mean is that we have this android.support.v4.app.Fragment for versions < Honeycomb and the fragment itself for Honeycomb and up. My question is that how does using either one of them affect the application in terms of efficieny and performance beside the backward compatibility. Also i keep seeing that the xml layout changes quite a bit depending on the support package that we use . Some have <fragment>, some have <fragmentActivity>, etc. How to know what is supposed to be used in the xml?
If someone can explain me in simple terms on how to go about using the fragment in different ways it would be highly appreciated.
To answer the first question there isn't any performance or efficiency issues with using either one of them beside the backward compatibility that you just mentioned.
For fragments that were added for API 11 and above there are three main classes:
android.app.Fragment
The base class for all fragment definitions
android.app.FragmentManager
The class for interacting with fragment objects inside an activity
android.app.FragmentTransaction
The class for performing an atomic set of fragment operations
And for fragments that were added for API 11 and below are:
android.support.v4.app.FragmentActivity
The base class for all activities using compatibility-based fragment (and loader) features
android.support.v4.app.Fragment
The base class for all fragment definitions
android.support.v4.app.FragmentManager
The class for interacting with fragment objects inside an activity
android.support.v4.app.FragmentTransaction
The class for performing an atomic set of fragment operations
If you are using the v4 support library then you need to make sure that you extend FragmentActivity by importing the android.support.v4.app.FragmentActivity and then in the xml you need to have a <fragment> tag with an android:name property where you define the complete class name.
More info: http://developer.android.com/training/basics/fragments/creating.html
I'm trying to implement an activity which will include TabHost and each tab in it will run\represent a different Activity so if the user press on tab #1 he'll see Activity A and if he'll press on tab #2 he'll see Activity B.
I don't want to use TabActivity as it been deprecated but the new API FragmentTabHost just doesn't seem to be the right answer\implementation (i need to support OS 2.2 and later).
Does anyone have a good idea\example how to do it?
Thx
Don't use TabHost anymore. Use the new ActionBar API with ActionBarSherlock.
To support Fragments down to Android 2.2 you have to use the Android Support Library.