In my MainActivity, I am using a android.support.v4.view.ViewPager with a android.support.v4.app.FragmentPagerAdapter. I have seen many questions like this one, except the answers say to use android.support.v13.app.FragmentPagerAdapter, which is now deprecated. What is the current, proper, up-to-date answer of how to include a android.preference.PreferenceFragment inside a android.support.v4.app.FragmentPagerAdapter?
Note: My minimum sdk version is 16.
Note 2: I'd prefer to use only android/google provided libraries, if possible.
I have no idea if this is possible, but can the PreferenceFragment be shown inside an android.support.v4.app.Fragment to combat this issue?
You can use the android.support.v7.preference.PreferenceFragmentCompat as an alternative to PreferenceFragment that works with the android.support.v4.app.FragmentPagerAdapter as discussed in this Google+ post
It is part of the v7 Preference Support Library.
Related
The current minSdkVersion for my app is 16, targetSdkVersion,compileSdkVersion are 21.
When I first started the app, minSdkVersion was 10, and the book I was learning from used the v4 Support library.
I've gone through my app and I found that these are the imports I have been using from the v4 support library:
ActionBarDrawerToggle
DialogFragment
DrawerLayout
Fragment
FragmentActivity
FragmentManager
FragmentStatePagerAdapter
ListFragment
NavUtils
ViewPager
Since I have raised the minSdkVersion, I don't think I need to use the support versions of most of these anymore. I believe I still have to use ViewPager and DrawerLayout, though.
The developer pages say:
Caution: When using classes from the Support Library, be certain you import the class from the appropriate package.
For example, when applying the ActionBar class:
android.support.v7.app.ActionBar when using the Support Library.
android.app.ActionBar when developing only for API level 11 or higher.
So, my question is:
1) May I simply use an import similar to android.app.Fragment for all of the above, since my API level is minimum 16? (Except ViewPager and DrawerLayout)
2) For ViewPager and DrawerLayout, is there a difference between using v4 or v7 or another support library version?
Any advice is appreciated. Thanks!
First, with Fragment and all its related paraphernalia (FragmentManager, FragmentTransaction, &c) the most important thing is to use the imports consistently (that is, don't mix and match). That, I think, is the point the documentation wants to make.
That said...
You could swap out usages of android.support.v4.app.Fragment with android.app.Fragment if you wanted to use the native versions of these classes instead of the support ones, but be careful: the support library also adds some methods and callbacks that were not present in API level 16 (one important example being getChildFragmentManager(). So, basically, it comes down to whether all the features of Fragments that you're using are available for the native Fragments in all the API levels you will support.
Also, using the support library Fragments means that they should behave exactly the same in all Android versions, while they might be differences in the native implementations.
In our particular case we decided to keep using the support library, but YMMV.
As for the second question, I don't think there is a v7-specific ViewPager (at least, I think so). The v7 libraries are add-ons (appcompat, gridlayout, &c).
I am implementing fragments with a DrawerLayout. I have 2 options when I import Fragments
android.support.v4.app
android.app
Which one should I use ? I dont see any difference except it seems like the android.support.v4.app has no support for objectAnimator.
What do you suggest ?
Edit: I only plan on supporting API level 14 and higher...
It depends on whether you are using Support Library.
If you are using fragments below api level 11 then use android.support.v4.app. In this case you will extend FragmentActivity which is the base class of support based fragments.
If you are using fragments in api level 11 and above use android.app. In this case you will extend standard Activity.
Take a look at the below link and decide on what versions your app should run. Depending on that decide whether you need support library or not.
https://developer.android.com/about/dashboards/index.html
I only plan on supporting API level 14 and higher...
Then there is no need to use support library. Use
import android.app.Fragment
and extend standard Activity.
If you are using support libary for drawerlayout then you should use android.support.v4.app for fragments.
You can use open-source "AndroidX" Support Library nowadays.
You can start from here AndroidX
I tried to use almost all examples and source code from internet and all of them just doesn't work. I got this warning "The type TabActivity is deprecated", anyone have source code for Tabhost that is really working and doesn't crash?
You should not be using Tabs anymore. Look after Fragments instead. a good place to start is
http://developer.android.com/guide/components/fragments.html
From the documentation for TabActivity:
This class was deprecated in API level 13.
New applications should use Fragments instead of this class; to continue to run on older devices, you can use the v4 support library which provides a version of the Fragment API that is compatible down to DONUT.
You should learn how to use Fragments instead. TabHost/TabActivity isn't being actively supported any more, so if you're just learning how to do tabs, you should do it the new way. No point in learning something, only to have to replace it anyway.
From the link here:
This class was deprecated in API level 13.
New applications should use Fragments instead of this class; to continue to run on older devices, you can use the v4 support library which provides a version of the Fragment API that is compatible down to DONUT.
So the suggestion is obvious - use Fragments. But don't ask here how, create a new Question.
go through the link:http://developer.android.com/training/backward-compatible-ui/abstracting.html Download the Sample App: TabCompat.zip
I'm trying to offer tab support for all Android versions 2.2 and above. Is there a way of achieving this without using any deprecated classes/methods?
The Problem is as following:
TabActivity is deprecated because it derives from ActivityGroup which is deprecated, too. Problems are, to start Activitys inside Tabs and don't break callbacks like onActivityResume. There are some 'Hacks' around which solve this but they are ALL relying on the deprecated LocalActivityManager. So I see no solution of using Tabs without ANY deprecated calls. Since Android 3.X google invented Fragments for this kind of things which are supported on lower Versions with the SupportPackage. You should definetly try them in Combination with the new ActionBar if you want to avoid deprecated classes/methods
EDIT:
Link to FragmentTabs for further reference
I recon with with the answer Rafael gave.
Instead of resorting to the TabActivity you can sill use the regular TabHost to show tabs in all versions of android.
I have defined dialogfragments (because Android documentation says that it is better and indeed it is) but now I want to use it in PreferenceActivity.
The problem is that I cannot use getSupportFragmentManager() there and I cannot use PreferenceFragment since it doesn't work with compatibility library. Looks like a road block scenario.
Can anyone advise on this?
As you noted, PreferenceFragment is not in the android compatibility package and unfortunately there is no clean way around this. For my own personal project, I had to adapt an implementation off of Android's source code:
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.0.4_r1.2/android/preference/PreferenceFragment.java
The answers to this question have some great suggestions:
Was PreferenceFragment intentionally excluded from the compatibility package?