B4A Library Amir_Fragment

Discussion in 'Additional libraries, classes and official updates' started by alimanam3386, Jan 6, 2019.

  1. alimanam3386

    alimanam3386 Active Member Licensed User

    Amir Fragment
    Android - Fragments

    A Fragment is a piece of an activity which enable more modular activity design. It will not be wrong if we say, a fragment is a kind of sub-activity.

    Following are important points about fragment −

    • A fragment has its own layout and its own behaviour with its own life cycle callbacks.

    • You can add or remove fragments in an activity while the activity is running.

    • You can combine multiple fragments in a single activity to build a multi-pane UI.

    • A fragment can be used in multiple activities.

    • Fragment life cycle is closely related to the life cycle of its host activity which means when the activity is paused, all the fragments available in the activity will also be stopped.

    • A fragment can implement a behaviour that has no user interface component.

    • Fragments were added to the Android API in Honeycomb version of Android which API version 11.
    You create fragments by extending Fragment class and You can insert a fragment into your activity layout by declaring the fragment in the activity's layout file, as a <fragment> element.

    Prior to fragment introduction, we had a limitation because we can show only a single activity on the screen at one given point in time. So we were not able to divide device screen and control different parts separately. But with the introduction of fragment we got more flexibility and removed the limitation of having a single activity on the screen at a time. Now we can have a single activity but each activity can comprise of multiple fragments which will have their own layout, events and complete life cycle.

    Following is a typical example of how two UI modules defined by fragments can be combined into one activity for a tablet design, but separated for a handset design.


    The application can embed two fragments in Activity A, when running on a tablet-sized device. However, on a handset-sized screen, there's not enough room for both fragments, so Activity A includes only the fragment for the list of articles, and when the user selects an article, it starts Activity B, which includes the second fragment to read the article.

    Fragment Life Cycle
    Android fragments have their own life cycle very similar to an android activity. This section briefs different stages of its life cycle.


    Fragment lifecycle
    Here is the list of methods which you can to override in your fragment class −

    • onAttach()The fragment instance is associated with an activity instance.The fragment and the activity is not fully initialized. Typically you get in this method a reference to the activity which uses the fragment for further initialization work.

    • onCreate() The system calls this method when creating the fragment. You should initialize essential components of the fragment that you want to retain when the fragment is paused or stopped, then resumed.

    • onCreateView() The system calls this callback when it's time for the fragment to draw its user interface for the first time. To draw a UI for your fragment, you must return a View component from this method that is the root of your fragment's layout. You can return null if the fragment does not provide a UI.

    • onActivityCreated()The onActivityCreated() is called after the onCreateView() method when the host activity is created. Activity and fragment instance have been created as well as the view hierarchy of the activity. At this point, view can be accessed with the findViewById() method. example. In this method you can instantiate objects which require a Context object

    • onStart()The onStart() method is called once the fragment gets visible.

    • onResume()Fragment becomes active.

    • onPause() The system calls this method as the first indication that the user is leaving the fragment. This is usually where you should commit any changes that should be persisted beyond the current user session.

    • onStop()Fragment going to be stopped by calling onStop()

    • onDestroyView()Fragment view will destroy after call this method

    • onDestroy()onDestroy() called to do final clean up of the fragment's state but Not guaranteed to be called by the Android platform.
    How to use Fragments?
    This involves number of simple steps to create Fragments.

    • First of all decide how many fragments you want to use in an activity. For example let's we want to use two fragments to handle landscape and portrait modes of the device.

    • Next based on number of fragments, create classes which will extend the Fragment class. The Fragment class has above mentioned callback functions. You can override any of the functions based on your requirements.

    • Corresponding to each fragment, you will need to create layout files in XML file. These files will have layout for the defined fragments.

    • Finally modify activity file to define the actual logic of replacing fragments based on your requirement.
    Types of Fragments
    Basically fragments are divided as three stages as shown below.

    • Single frame fragments − Single frame fragments are using for hand hold devices like mobiles, here we can show only one fragment as a view.

    • List fragments − fragments having special list view is called as list fragment

    • Fragments transaction − Using with fragment transaction. we can move one fragment to another fragment.

    Additional plugins
    • Amir_PaperBoarding

    - source of upper descriptions

    Basic Demo
    Fragment with animation
    MultiFragment ( used Amir_RecyclerView )
    Shared Elements ( used Amir_RecyclerView )
    View Pager ( used Amir_RecyclerView )

    Source of samples


    You can have this fantastic library just with $15

    Make a payment

    after your payment please send me an email [ alimanam@gmail.com ]
    Last edited: Mar 16, 2019
  2. mshafiee110

    mshafiee110 Active Member Licensed User

    Excellent :)
  3. asales

    asales Well-Known Member Licensed User

    @alimanam3386 I sent to you an email with information about the payment by Paypal.
    alimanam3386 likes this.
  4. alimanam3386

    alimanam3386 Active Member Licensed User

    thank you for your support. please check your email.
    asales likes this.
  5. alimanam3386

    alimanam3386 Active Member Licensed User

    Load layout by designer.

    Sub Class_Globals
    Public Fragment As Amir_Fragment
    Public TAG As String = "Third Fragment"
    Private Button1 As Button
    End Sub

    Public Sub Initialize
    End Sub

    Private Sub Amir_onCreateView (Parent As Panel)
    End Sub

    Private Sub Amir_onCreate(SavedInstanceState As Object)
    " Created",Colors.Red)
    End Sub

    Private Sub Amir_onPause
    " Paused",Colors.Red)
    End Sub

    Sub Button1_Click
    Msgbox("Hello" , "")
    End Sub

    Attached Files:

    asales likes this.
  6. asales

    asales Well-Known Member Licensed User

    Works fine. Thanks.
  7. alimanam3386

    alimanam3386 Active Member Licensed User

    Do animation by click on a view ( ex: button )

    #Extends : android.support.v4.app.FragmentActivity
    Sub Process_Globals
    End Sub

    Sub Globals
    Dim Content As Panel
    End Sub

    Sub Activity_Create(FirstTime As Boolean)
    Dim button As Button
    button.Text = "Animation"
    Activity.AddView(button , 20dip , 100%y - 50dip , 100%x - 40dip , 40dip)
    End Sub

    Sub btnAnimation_Click
    Dim MyF2 As MyFragment
    Dim Anim As Amir_FragmentAnimation
    Dim FT As Amir_FragmentTransaction
    End Sub

    Attached Files:

    Last edited: Feb 18, 2019
  8. itgirl

    itgirl Active Member Licensed User

    Hello, thank you for this Lib which is very handy, I have some questions though

    1- if i have a normal activity [main] which i have an imageview in it when clicked on i need to open a Fragments with the imageview as shared element also with animation. the animation does not seem to work from Activity->Fragments but from a Fragments->Fragments it works [same as your example] any ideas why is so ?

    2- in a fragment event onCreateView we have a panel but how can we Initialize this panel in order to consume the click events?? because the panel click event is not handled so all views under this parent panel will be clickable of course we can always add a panel to the parent panel and Initialize it with click events in order to consume the event but however i dont think this is best practice
  9. alimanam3386

    alimanam3386 Active Member Licensed User

    For question#2 test below code:

    Dim r As Reflector = Parent

    Sub Parent_Click(ViewTag As Object)
    Log("Parent Clicked!")
    End Sub
    for question#1 : I try to find a solution for it :cool:
    itgirl likes this.
  10. itgirl

    itgirl Active Member Licensed User

    Yeah setting the click listener is also a solution and i think its better than adding another panel over the parent ;)
    for question #1 I'm trying to find a solution within B4A if i can ;)
    alimanam3386 likes this.
  11. itgirl

    itgirl Active Member Licensed User

    Any updates on the issue ? I have found another issue
    if you have Activity_KeyPress (KeyCode As Int) in the main activity then addToBackStack would have no effect whatsoever and can not go back to the first fragment you can try it on the shared elements example

    I've added
    Dim FTM As Amir_FragmentManager
    If FTM.BackStackEntryCount > 0 Then
    Return True
    End If
    in Activity_KeyPress for it to work but i thought addtobackstack will handle all this for me.

    ALSO still exit animation is not working using this method
    Last edited: Mar 7, 2019
  12. alimanam3386

    alimanam3386 Active Member Licensed User

    New Amir_Fragment plugin :

    Amir_PaperBoarding is a material design UI slider.
    Github : https://github.com/Ramotion/paper-onboarding-android

    Paper Onboarding is a simple and easy to use onboarding slider for your app. You just need to provide content for each slider page - a main icon, text, and small round icon for the bottom.

    Requirements : Android 4.0.3 IceCreamSandwich (API lvl 15) or greater

    Download link sent to buyers of amir fragment for free‍‍!

    Last edited: Mar 16, 2019
  13. ocalle

    ocalle Active Member Licensed User

    Awesome! testing.-
  14. ocalle

    ocalle Active Member Licensed User

    Are you included the plugin on library? to buy one
  15. alimanam3386

    alimanam3386 Active Member Licensed User

    this plugin is addition lib for main lib ( Amir_Fragment) and anyone buy the main lib can have this and further plugins.
    ocalle likes this.
  16. asales

    asales Well-Known Member Licensed User

    Received, tested and works fine. Thanks!
    alimanam3386 likes this.
  17. ocalle

    ocalle Active Member Licensed User

    Payment and email sent!:)
    alimanam3386 likes this.
  18. alimanam3386

    alimanam3386 Active Member Licensed User

    please check your emailbox. thanks for your support.
    ocalle likes this.
  19. asales

    asales Well-Known Member Licensed User

  20. itgirl

    itgirl Active Member Licensed User

    Any update on the issue here #8 ? First question
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice