An library to help android developers working easly with activities and fragments (Kotlin version)

Related tags

Kotlin Projects AFM
Overview

AFM

Download Build Status APILicense: MIT Awesome Kotlin Badge

An library to help android developer working easly with activities and fragments (Kotlin)

Motivation

  • Accelerate the process and abstract the logic of opening, adding and replacing fragments in an activity;
  • Reduce the number of activities declared in the project;
  • Get access to Activity::onBackPressed() inside of the fragments.
  • Add animated transitions between fragments in an easy way;
  • Easy way to work with shared elements;
An animated GIF showing navigation flow An animated GIF showing shared elements working An animated GIF showing onbackpressed working

Download

To use the AFM, add the compile dependency with the latest version.

Gradle

Add the AFM to your build.gradle:

dependencies {
    compile 'com.massivedisaster:afm:0.0.1'
}

Maven

In the pom.xml file:

<dependency>
    <groupId>com.massivedisaster</groupId>
    <artifactId>afm</artifactId>
    <version>0.0.1</version>
</dependency>

Usage

1. Create your Activity

Create a new activity and extends the BaseActivity.

class ActivityPrimaryTheme : BaseActivity() {

    // The layout resource you want to find the FrameLayout.
    override fun layoutToInflate(): Int {
        return R.layout.activity_fullscreen
    }

    // The FrameLayout id you want to inject the fragments.
    override fun getContainerViewId(): Int {
        return R.id.frmContainer
    }
}

Create the layout to be used by your AbstractFragmentActivity.

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/frmContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

2. Opening, adding or replacing fragments in your AbstractFragmentActivity.

Open a new AbstractFragmentActivity with a fragment.

ActivityCall.init(context, ActivityPrimaryTheme::class, FragmentExample::class)
                .build()

Add a new Fragment in the actual AbstractFragmentActivity.

FragmentCall.init(activity, FragmentExample::class)
                .build()

Replace a new Fragment in the actual AbstractFragmentActivity.

FragmentCall.init(activity as BaseActivity, FragmentExample::class)
                .setTransitionType(FragmentCall.TransitionType.REPLACE)
                .build()

3. Default Fragment

You can set a default fragment in you BaseActivity. An example, if your BaseActivity is started by an external intent you need to define a default fragment.

class ActivityPrimaryTheme : BaseActivity() {
    ...

    override fun getDefaultFragment(): KClass<out Fragment>? {
        return FragmentSplash::class
    }
}

4. Fragment Transaction Animations.

When you add or replace fragments in the old way you can set a custom animations for the transactions. So, you can set custom animation in easly way using this library.

Single Transaction Animation

If you want to add a single animation only for one transaction you can do this:

FragmentCall.init(activity as BaseActivity, FragmentAddReplace::class)
    .setTransitionType(FragmentCall.TransitionType.ADD)
    .setTransactionAnimation(object : TransactionAnimation {
        override val animationEnter: Int
            get() = R.anim.enter_from_right

        override val animationExit: Int
            get() = R.anim.exit_from_left

        override val animationPopEnter: Int
            get() = R.anim.pop_enter

        override val animationPopExit: Int
            get() = R.anim.pop_exit
    }).build()

Attention: This only works in transactions between fragments, i.e. add and replace

Custom animation for all transactions.

If you want to add a custom animation for all transactions inside of a AbstractFragmentActivity you can override the follow methods:

abstract class ActivityPrimaryTheme : BaseActivity() {

    ...

    override val animationEnter: Int
        get() = android.R.anim.fade_in

    override val animationExit: Int
        get() = android.R.anim.fade_out

    override val animationPopEnter: Int
        get() = android.R.anim.fade_in

    override val animationPopExit: Int
        get() = android.R.anim.fade_out

}

5. Shared Elements

If you want to make your app beautiful you need to put some cool animation on it! Shared elements are introduce in API 21 and makes the transactions so great and sweet. So, now it's very easy to share elements between fragments or activities. Let's take a look:

Activity A

...
.addSharedElement(view, "sharedElement")
...
.build()

Activity B

ViewCompat.setTransitionName(view, "sharedElement")

or

<View
  ...
  android:transitionName="sharedElement" />

Attention: Shared elements doesn't work when you use add! Well if you remove the first fragment it's possible, i.e. a replace :)

6. Custom Intents

Sometimes you want to add more information to the Intent or set some flags. You can use the follow method to open a new BaseActivity:

.setFlags(flags)

7. Fragment#OnBackPressed

Allows to have back pressed events in Fragments.

class FragmentOnBackPressed : Fragment(), OnBackPressedListener {

    ...

    @Override
    override fun onBackPressed(): Boolean {
      // Do what you want here! If you return true the activity will not process the OnBackPressed
    }

}

Goodies

  • You can pass a tag to be applied in the Fragment.
  • You can pass REQUEST_CODE to the startActivityForResult.
  • You can addToBackStack.
  • You can pass data between fragments using a Bundle.
  • You can get access to the original FragmentTransaction.
  • You can use DataBinding in your DataBindingBaseActivity, all you need is override initializeDataBinding() and bind the view!

Sample

Sample app can be found in the sample module. Alternatively, you can use dryrun to run the sample.

The Sample app don't require any configuration to interact.

Contributing

CONTRIBUTING

License

MIT LICENSE

You might also like...
Super Heroes Kata for Android Developers in Kotlin. The main goal is to practice UI Testing.
Super Heroes Kata for Android Developers in Kotlin. The main goal is to practice UI Testing.

KataSuperHeroes in Kotlin We are here to practice UI Testing. We are going to use Espresso to interact with the Application UI. We are going to use Ko

Maxibon kata for Kotlin Developers. The main goal is to practice property based testing.
Maxibon kata for Kotlin Developers. The main goal is to practice property based testing.

Kata Maxibon for Kotlin. We are here to practice property based testing. We are going to use KotlinTest to write our tests. We are going to practice p

TODO API Client Kata for Kotlin Developers. The main goal is to practice integration testing using MockWebServer
TODO API Client Kata for Kotlin Developers. The main goal is to practice integration testing using MockWebServer

KataTODOApiClient for Kotlin We are here to practice integration testsing using HTTP stubbing. We are going to use MockWebServer to simulate a HTTP se

A pragmatic lightweight dependency injection framework for Kotlin developers.
A pragmatic lightweight dependency injection framework for Kotlin developers.

A pragmatic lightweight dependency injection framework for Kotlin developers. Koin is a DSL, a light container and a pragmatic API

BindsAdapter is an Android library to help you create and maintain Adapter class easier via ksp( Kotlin Symbol Processing).

BindsAdapter BindsAdapter is an Android library to help you create and maintain Adapter class easier via ksp( Kotlin Symbol Processing). Installation

Repo: Programming problems with solutions in Kotlin to help avid Kotlin learners to get a strong hold on Kotlin programming.

Kotlin_practice_problems Repo: Programming problems with solutions in Kotlin to help avid Kotlin learners to get a strong hold on Kotlin programming.

Flexible switch is a responsive switch with some nice features which developers can use for making awesome switches on android platform.

flexible-switch It is a responsive switch in android, it can resize itself according to its size. It's recommended to use it with ConstraintLayout to

RCZ algorithm in kotlin (update version)

RCZEncryptationKT RCZ Encrypt uses maps to encrypt your string this use ALPHABYTE to to view the bytearray of encoded strings This use random chars by

Android library to help enter, and more importantly, leave, android "Immersive Mode".

Immersive Lock Android has various ways to full screen an application including locking the screen to prevent accidentally leaving the app. This is pa

Owner
Massive Disaster
Massive Disaster
🔥The Android Startup library provides a straightforward, performant way to initialize components at the application startup. Both library developers and app developers can use Android Startup to streamline startup sequences and explicitly set the order of initialization.

??The Android Startup library provides a straightforward, performant way to initialize components at the application startup. Both library developers and app developers can use Android Startup to streamline startup sequences and explicitly set the order of initialization.

Rouse 1.3k Dec 30, 2022
Sample app to demonstrate the integration code and working of Dyte SDK for android, using Kotlin.

Dyte Kotlin Sample App An example app in kotlin using the Dyte Mobile SDK Explore the docs » View Demo · Report Bug · Request Feature Table of Content

Dyte 8 Dec 3, 2021
NFC NDEF Reader and Writer. there are two separate activities for reading and writing.

kotlin-nfc-sample NFC NDEF Reader and Writer. there are two separate activities for reading and writing. Steps Clone the Project Open with Android Stu

null 2 Oct 1, 2022
An Android instance app for working with Google Map, Kotlin

map-instant-app An Android instance app Android technologies that I used: Name Description 1 Kotlin 2 Coroutine 3 Navigation 4 DataBinding 5 ViewBindi

Malihe 3 Nov 14, 2022
An Android instance app for working with Google Map, Kotlin

map-instance-app A map instance app for seeing the current position of the user and saving that in the database and showing a list of saved locations.

Malihe 2 Nov 3, 2022
Basic RestAPI to practice my skills working with Spring/Kotlin/Gradle

Dining Review API Dining Review API is a basic RestAPI roughly based on the requirements given in a Milestone project in the Building REST APIs with J

Jonas Kuhlo 0 Nov 18, 2021
(National Service Scheme) displays a list of activities organised by a committee

NSS NSS (National Service Scheme) displays a list of activities organised by a c

Krish Parekh 21 Feb 14, 2022
A small application for working with the Github API, made as a practical task. GeekBrains, course of study: Popular libraries: RxJava 2, Dagger 2, Moxie.

GeekBrains_Course_AndroidOnKotlin_HW_My_Movie Домашнее задание к занятию №2-6 Студента GeekBrains Веремеенко Дмитрия Факультет: Android-разработки Кур

Dmitriy 3 Aug 24, 2021
Integration Testing Kotlin Multiplatform Kata for Kotlin Developers. The main goal is to practice integration testing using Ktor and Ktor Client Mock

This kata is a Kotlin multiplatform version of the kata KataTODOApiClientKotlin of Karumi. We are here to practice integration testing using HTTP stub

Jorge Sánchez Fernández 29 Oct 3, 2022
Screenshot Kata for Android Developers with Kotlin. The main goal is to practice UI Screenshot Testing.

KataScreenshot in Kotlin We are here to practice UI testing using screenshot tests for Android. We are going to use Espresso to interact with the Appl

Karumi 76 Nov 20, 2022