Fragments

class CardViewActivity : Activity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_card_view)
        if (savedInstanceState == null) {
            fragmentManager.beginTransaction()
                    .add(R.id.container, CardViewFragment())
                    .commit()
        }
    }

}
class CardViewFragment : Fragment() {

    private val TAG = "CardViewFragment"

    // The [CardView] widget.
    @VisibleForTesting lateinit var cardView: CardView

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        return inflater.inflate(R.layout.fragment_card_view, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        cardView = view.findViewById(R.id.cardview)

        // add listners
}

The component that interacts with fragments from your current activity is the fragment manager.

<?xml version="1.0" encoding="UTF-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.example.android.cardview.CardViewActivity"
    tools:ignore="MergeRootFrame" />

There are some additional methods that activities do not have:

  • onAttach(): This is executed when a fragment is associated to an activity.
  • onCreateView(): This instantiates and returns a fragment's view instance.
  • onActivityCreated(): This executes when an activity's onCreate() is executed.
  • onDestroyView(): This executes when a view is destroyed; it is convenient when some cleanup is needed.
  • onDetach(): This is executed when a fragment is unassociated from an activity. To demonstrate the use of fragments, we will put the central part of our MainActivity into one single fragment. Later, we will move it to ViewPager and add more pages to it.

Best practive is to create a reusable base Fragment:

abstract class BaseFragment : Fragment() { 
      protected abstract val logTag : String 
      protected abstract fun getLayout(): Int 

    override fun onCreateView( 
      inflater: LayoutInflater?, container: ViewGroup?,
      savedInstanceState: Bundle? 
      ): View? { 
        Log.d(logTag, "[ ON CREATE VIEW ]") 
        return inflater?.inflate(getLayout(), container, false) 
     } 

     override fun onPause() { 
        super.onPause() 
        Log.v(logTag, "[ ON PAUSE ]") 
     } 

     override fun onResume() { 
        super.onResume() 
        Log.v(logTag, "[ ON RESUME ]") 
     } 

     override fun onDestroy() { 
        super.onDestroy() 
        Log.d(logTag, "[ ON DESTROY ]") 
     } 

}

Use:

class ItemsFragment : BaseFragment() { 
    override val logTag = "Items fragment" 
    override fun getLayout(): Int { 
        return R.layout.fragment_items 
    } 
} 

class MainActivity : BaseActivity() { 

      override val tag = "Main activity" 
      override fun getLayout() = R.layout.activity_main 
      override fun getActivityTitle() = R.string.app_name 

      override fun onCreate(savedInstanceState: Bundle?) { 
        super.onCreate(savedInstanceState) 
        val fragment = ItemsFragment() 
        supportFragmentManager 
                .beginTransaction() 
                .add(R.id.fragment_container, fragment) 
                .commit() 
     } 
    }

results matching ""

    No results matching ""