Simple Android framework focused on communication between components (Activity
and Fragment
) including managing their state.
compile "com.github.grzechu92:frogment:1.0.0"
Library is published in jitpack.io
repository, if it isn't available in your project, add lines below to matching build.gradle
file.
repositories {
maven { url 'https://jitpack.io' }
}
You will need to bind Frogment
to application. Library uses Android component lifecycle callbacks to manage objects state. Simple default configuration looks like this:
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
new FrogmentFoundation(this);
}
}
If you'd like to change default behavior use configured Config
object:
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
final Config config = new Config.Builder()
.fragmentInstanceProvider(new ReflectionFragmentInstanceProvider()) //any class implementing FragmentInstanceProvider interface
.callActivityFinishOnEmptyBackStack(true) //boolean
.build();
new FrogmentFoundation.Builder(this)
.config(config)
.build();
}
}
We have got here one activity called MainActivity
and two fragments FragmentA
and FragmentB
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" />
</LinearLayout>
Notice fragment_container
ID of element where fragments will be injected using FragmentManager
public class Activity extends FrogmentActivity {
public Activity() {
super(R.id.fragment_container);
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_with_container);
}
@Override
protected FrogmentData getDefaultFrogmentData() {
return FrogmentData.forClass(FragmentA.class);
}
}
We need to extend FrogmentActivity
class that contains necessary boilerplate code. FrogmentActivity
has constructor where we need to pass container layout ID, in this case fragment_container
. Last important thing there is required method getDefaultFrogmentData
which provides default FrogmentData
that Activity should render if nothing else is explicitly defined (check app
sample application for more details)
public class FragmentA extends Frogment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_a_layout, container, false);
}
}
Nothing fancy there, instead of extending Fragment
class, we will have to implement Frogment
class provided by library.
At this moment we should have FragmentA
displayed in MainActivity
layout without as much boilerplate as using raw Android implementation.
According to previous example with MainActivity
, FragmentA
and FragmentB
at some point we will need to switch fragments, because user may get bored of FragmentA
content. To perform switch operation from fragment perspective we need to call:
final FrogmentData data = FrogmentData.forClass(FragmentB.class);
getFrogmentActivity().switchFrogment(data);
Method getFrogmentActivity()
is provided by Frogment
object that we extended instead of Android Fragment
one. It's all about passing FrogmentData
with data about target fragment to switchFrogment()
method. Library will handle the rest.
Instead of calling getFrogmentActivity().switchFrogment()
with FragmentData
, just call:
final FrogmentData data = FrogmentData.forClass(FragmentB.class);
switchFrogment(data);
Because we are already in FrogmentActivity
context.