FragmentTransactionManager is an Open Source Android library that allows developers to easily create applications with FragmentTransaction. Feel free to use it all you want in your Android apps provided that you cite this project and include the license in your app.
This lib will help you to have differents stacks of fragments by Activity. With those stacks you will be abel to have a navigation like iOS NavigationController.
//1 - You need to create a tag reference
// - Add on it the ressourceID corresponding to the correct content
// - You need set up the number of fragments will stay alive and detached,
// before be removed and saved into its stack
mFragmentTransactionBuilder.createTag("Menu", R.id.menuContent, 1);
//2 - Your fragment ex:"MainMenuFragment" HAVE TO EXTENT FTFragment
// - When you instantiate you fragment you need to add:
// - the context
// - the path of the fragment
// - a Bundle
// - an Animation when the fragment start
// - an Animation when the fragment leave
MainMenuFragment mFragment = FTFragment.instantiate(this, MainMenuFragment.class.getName(), null, Animation.ANIM_NONE, Animation.ANIM_NONE);
mFragmentTransactionBuilder.addFragmentInStack("Menu", mFragment);
//3 - To go back to the preview fragment you need to call back the correct tag (ex: "Menu")
mFragmentTransactionBuilder.removeTopFragmentInStackWithAnimation("Menu", true);
//4 - If differente tags are using the same ressourceID. You need just call back the correct tag to show the last Fragment again
mFragmentTransactionBuilder.showTopFragmentInStack("Menu");
Here's an video of the example application in this repository : YoutTube Video - comming soon[2]
- In Eclipse, just import the library as an Android library project. Project > Clean to generate the binaries you need, like R.java, etc.
- Then, just add FragmentTransactionManager as a dependency to your existing project and you're good to go!
In order to integrate FragmentTransactionManager into your own projects.
1. You need to set into your layout (xml) as a ROOT 'FTRelativeLayout' or 'FTLinearLayout'. You don't need to set the both layout, just one will be enough.
RelavitveLayout
<com.dovi.fragmentTransaction.layout.FTRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- ADD THE REST OF YOUR LAYOUT -->
<com.dovi.fragmentTransaction.layout.FTRelativeLayout/>
LinearLayout
<com.dovi.fragmentTransaction.layout.FTLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- ADD THE REST OF YOUR LAYOUT -->
<com.dovi.fragmentTransaction.layout.FTLinearLayout/>
2. Into you activity you need to init your layout
private FTRelativeLayout mRelativeLayout;
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.main_activity);
mRelativeLayout = (FTRelativeLayout) findViewById(R.id.root);
}
3. This part is very important, you have to check, before to add any stack or fragment into your FragmentTransactionManager, if
FragmentTransactionManager need to be restored. For that check the code below. For info, you HAVE TO init FragmentTransactionManager into
onResume()
@Override
protected void onResume() {
super.onResume();
//Here you init FragmentTransactionManager
mFragmentTransactionManager = mRelativeLayout.getFragmentManger(getSupportFragmentManager(), this);
//To not re-init your stacks check if it contain it
if (!mFragmentTransactionManager.isContainTag("Menu")) {
//Before add a fragment into a stack you need to init it
mFragmentTransactionManager.createTag("Menu", R.id.menuContent, 1);
mFragmentTransactionManager.createTag("ContentTab1", R.id.fragmentContent, 1);
mFragmentTransactionManager.addFragmentInStack("Menu", FTFragment.instantiate(this, MainMenuFragment.class.getName(), null, Animation.ANIM_NONE, Animation.ANIM_NONE));
Bundle mBundle = new Bundle();
mBundle.putString("title", "Tab1");
mFragmentTransactionManager.addFragmentInStack("ContentTab1", FTFragment.instantiate(this, MainContentFragment.class.getName(), mBundle, Animation.ANIM_NONE, Animation.ANIM_NONE));
}
}
- I'm still developing this lib so you may have some bugs...
- Edouard Roussillon
Copyright 2014 Edouard Roussillon
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.