Android Practice Note (3) --- Fragment parsing with app package or v4 package, android --- fragment
Android Practice Notes (3) --- Analysis of Fragment app package or v4 package
1) Problem Description
I believe that many of my friends will encounter the following situation when calling Fragment:
This is a problem...
Is Fragment under android. app or Fragment under android. support. v4.app?
A: Actually, they can all be. Yes, they can all be !!! We all know that Fragment was introduced after 3.0 (API 11 ).
What if the developed app needs to run in versions earlier than 3.0? For example, there is another 2.3 market share!
As a result, the v4 package came into being, and can be compatible with version 1.6 at least!
As for which package you want to use, there is not much market share for 3.0 mobile phones.
More than 4.0, so you can directly use the Fragment under the app package, and then call the relevant method.
Generally, there is no problem;If you use the app package for Fragment, FragmentManager and FragmentTransaction
All of them must be app packages.
Either use all apps or use all v4, or an error will be reported!
Of course, if you want your app to be compatible with earlier versions of mobile phones, you can choose to use the v4 package.
But there are a lot to note when using v4 packages!
2) Notes for using v4 packages:
① If you use the Fragment under the v4 package, the Activity where you are located will inherit the FragmentActivity!
Case: Today, fragment is loaded statically in the xml file, and then the Fragment is rewritten. However, an error is reported during Activity loading,
The following message indicates whether the Fragment error can be found or not. The name attribute has been changed several times or wrong! I finally found that it was used.
For the v4 package, you only need to change your Activity to FragmentActivity!
② This Code cannot be used today. The error is as follows:
It's a bit confusing. Fragment, FragmentManager, and FragmentTransaction all use v4 packages,
Does Activity inherit FragmentActivity? It can be changed to the app package, but this is not the same as the v4 package.
Does the prerequisite conflict? Actually, there are solutions?
A: You only need to change getFragmentManager () to getSupportFragmentManager ().
This is the problem with fragment in v4 package. In the future, the record will be updated.
If you have any questions and solutions, you can also tell the pig that sharing is a good virtue for programmers! Thank you!