Skip to content

Commit 77ceb1d

Browse files
authored
Update README.md
1 parent 996aecd commit 77ceb1d

File tree

1 file changed

+53
-138
lines changed

1 file changed

+53
-138
lines changed

README.md

Lines changed: 53 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,35 @@ With [Material Design Bottom Navigation pattern](https://www.google.com/design/s
1111
## Gradle
1212

1313
```groovy
14-
implementation 'com.ncapdevi:frag-nav:2.4.0' //or or `compile` if on older gradle version
14+
implementation 'com.ncapdevi:frag-nav:3.0.0' //or or `compile` if on older gradle version
1515
```
1616

1717
## How do I implement it?
1818

1919
### Initialize using a builder and one of two methods
20-
```java
21-
builder = FragNavController.newBuilder(savedInstanceState, getSupportFragmentManager(), R.id.container);
20+
```kotlin
21+
private val fragNavController: FragNavController = FragNavController(supportFragmentManager, R.id.container)
2222
```
2323
#### 1.
2424
Create a list of fragments and pass them in
25-
```java
26-
List<Fragment> fragments = new ArrayList<>(5);
27-
28-
fragments.add(RecentsFragment.newInstance());
29-
fragments.add(FavoritesFragment.newInstance());
30-
fragments.add(NearbyFragment.newInstance());
31-
fragments.add(FriendsFragment.newInstance());
32-
fragments.add(FoodFragment.newInstance());
33-
34-
builder.rootFragments(fragments);
25+
```kotlin
26+
val fragments = listOf(
27+
RecentsFragment.newInstance(0),
28+
FavoritesFragment.newInstance(0),
29+
NearbyFragment.newInstance(0),
30+
FriendsFragment.newInstance(0),
31+
FoodFragment.newInstance(0),
32+
RecentsFragment.newInstance(0),
33+
FavoritesFragment.newInstance(0),
34+
NearbyFragment.newInstance(0),
35+
FriendsFragment.newInstance(0),
36+
FoodFragment.newInstance(0),
37+
RecentsFragment.newInstance(0),
38+
FavoritesFragment.newInstance(0))
39+
40+
fragNavController.rootFragments = fragments
3541
```
42+
3643
#### 2.
3744

3845

@@ -43,32 +50,29 @@ public class YourActivity extends AppCompatActivity implements FragNavController
4350
```
4451

4552
```java
46-
builder.rootFragmentListener(this, 5)
53+
fragNavController.rootFragmentListener = this
4754
```
4855

49-
```java
56+
```kotlin
5057

51-
@Override
52-
public Fragment getRootFragment(int index) {
53-
switch (index) {
54-
case INDEX_RECENTS:
55-
return RecentsFragment.newInstance(0);
56-
case INDEX_FAVORITES:
57-
return FavoritesFragment.newInstance(0);
58-
case INDEX_NEARBY:
59-
return NearbyFragment.newInstance(0);
60-
case INDEX_FRIENDS:
61-
return FriendsFragment.newInstance(0);
62-
case INDEX_FOOD:
63-
return FoodFragment.newInstance(0);
58+
override val numberOfRootFragments: Int = 5
59+
60+
override fun getRootFragment(index: Int): Fragment {
61+
when (index) {
62+
INDEX_RECENTS -> return RecentsFragment.newInstance(0)
63+
INDEX_FAVORITES -> return FavoritesFragment.newInstance(0)
64+
INDEX_NEARBY -> return NearbyFragment.newInstance(0)
65+
INDEX_FRIENDS -> return FriendsFragment.newInstance(0)
66+
INDEX_FOOD -> return FoodFragment.newInstance(0)
6467
}
65-
throw new IllegalStateException("Need to send an index that we know");
68+
throw IllegalStateException("Need to send an index that we know")
6669
}
70+
6771
```
6872

6973
#### 3.
7074
```java
71-
mFragNavController = builder.build();
75+
fragNavController.initialize(FragNavController.TAB3, savedInstanceState)
7276
```
7377

7478
### SaveInstanceState
@@ -77,13 +81,11 @@ Send in the supportFragment Manager, a list of base fragments, the container th
7781
After that, you have four main functions that you can use
7882
In your activity, you'll also want to override your onSaveInstanceState like so
7983

80-
```java
81-
@Override
82-
protected void onSaveInstanceState(Bundle outState) {
83-
super.onSaveInstanceState(outState);
84-
if (mNavController != null) {
85-
mNavController.onSaveInstanceState(outState);
86-
}
84+
```kotlin
85+
override fun onSaveInstanceState(outState: Bundle?) {
86+
super.onSaveInstanceState(outState)
87+
fragNavController.onSaveInstanceState(outState!!)
88+
8789
}
8890
```
8991

@@ -129,24 +131,25 @@ You can only replace onto the currently selected index
129131
### Transaction Options
130132
All of the above transactions can also be done with defined transaction options.
131133
The FragNavTransactionOptions have a builder that can be used.
132-
```java
133-
public class FragNavTransactionOptions {
134-
List<Pair<View, String>> sharedElements;
134+
```kotlin
135+
class FragNavTransactionOptions private constructor(builder: Builder) {
136+
val sharedElements: List<Pair<View, String>> = builder.sharedElements
135137
@FragNavController.Transit
136-
int transition = FragmentTransaction.TRANSIT_NONE;
138+
val transition = builder.transition
137139
@AnimRes
138-
int enterAnimation = 0;
140+
val enterAnimation = builder.enterAnimation
139141
@AnimRes
140-
int exitAnimation = 0;
142+
val exitAnimation = builder.exitAnimation
141143
@AnimRes
142-
int popEnterAnimation = 0;
144+
val popEnterAnimation = builder.popEnterAnimation
143145
@AnimRes
144-
int popExitAnimation = 0;
146+
val popExitAnimation = builder.popExitAnimation
145147
@StyleRes
146-
int transitionStyle = 0;
147-
String breadCrumbTitle;
148-
String breadCrumbShortTitle;
149-
}
148+
val transitionStyle = builder.transitionStyle
149+
val breadCrumbTitle: String? = builder.breadCrumbTitle
150+
val breadCrumbShortTitle: String? = builder.breadCrumbShortTitle
151+
val allowStateLoss: Boolean = builder.allowStateLoss
152+
150153
```
151154
152155
### Get informed of fragment transactions
@@ -157,97 +160,9 @@ A sample application is in the repo if you need to see how it works.
157160
158161
### Fragment Transitions
159162
160-
Use FragNavController.setTransitionMode();
161-
162-
### Helper functions
163-
```java
164-
/**
165-
* Get the number of fragment stacks
166-
*
167-
* @return the number of fragment stacks
168-
*/
169-
@CheckResult
170-
public int getSize() {
171-
return fragmentStacks.size();
172-
}
173-
174-
/**
175-
* Get a copy of the stack at a given index
176-
*
177-
* @return requested stack
178-
*/
179-
@SuppressWarnings("unchecked")
180-
@CheckResult
181-
@Nullable
182-
public Stack<Fragment> getStack(@TabIndex int index) {
183-
if (index == NO_TAB) {
184-
return null;
185-
}
186-
if (index >= fragmentStacks.size()) {
187-
throw new IndexOutOfBoundsException("Can't get an index that's larger than we've setup");
188-
}
189-
return (Stack<Fragment>) fragmentStacks.get(index).clone();
190-
}
191-
192-
/**
193-
* Get a copy of the current stack that is being displayed
194-
*
195-
* @return Current stack
196-
*/
197-
@SuppressWarnings("unchecked")
198-
@CheckResult
199-
@Nullable
200-
public Stack<Fragment> getCurrentStack() {
201-
return getStack(selectedTabIndex);
202-
}
203-
204-
/**
205-
* Get the index of the current stack that is being displayed
206-
*
207-
* @return Current stack index
208-
*/
209-
@CheckResult
210-
@TabIndex
211-
public int getCurrentStackIndex() {
212-
return selectedTabIndex;
213-
}
214-
215-
/**
216-
* @return If true, you are at the bottom of the stack
217-
* (Consider using replaceFragment if you need to change the root fragment for some reason)
218-
* else you can popFragment as needed as your are not at the root
219-
*/
220-
@CheckResult
221-
public boolean isRootFragment() {
222-
Stack<Fragment> stack = getCurrentStack();
223-
224-
return stack == null || stack.size() == 1;
225-
}
226-
227-
/**
228-
* Helper function to get wether the fragmentManger has gone through a stateSave, if this is true, you probably want to commit allowing stateloss
229-
*
230-
* @return if fragmentManger isStateSaved
231-
*/
232-
public boolean isStateSaved() {
233-
return fragmentManger.isStateSaved();
234-
}
163+
Can be set using the transactionOptions
235164
236-
/**
237-
* Use this if you need to make sure that pending transactions occur immediately. This call is safe to
238-
* call as often as you want as there's a check to prevent multiple executePendingTransactions at once
239-
*
240-
*/
241-
public void executePendingTransactions() {
242-
if (!executingTransaction) {
243-
executingTransaction = true;
244-
fragmentManger.executePendingTransactions();
245-
executingTransaction = false;
246-
}
247-
}
248165
249-
250-
```
251166
252167
## Restoring Fragment State
253168
Fragments transitions in this library use attach()/detch() (http://daniel-codes.blogspot.com/2012/06/fragment-transactions-reference.html). This is a delibrate choice in order to maintain the fragment's lifecycle, as well as being optimal for memory. This means that fragments will go through their proper lifecycle https://developer.android.com/guide/components/fragments.html#Lifecycle . This lifecycle includes going through `OnCreateView` which means that if you want to maintain view states, that is outside the scope of this library, and is up to the indiviudal fragment. There are plenty of resources out there that will help you design your fragments in such a way that their view state can be restored https://inthecheesefactory.com/blog/fragment-state-saving-best-practices/en and there are libraries that can help restore other states https://github.com/frankiesardo/icepick

0 commit comments

Comments
 (0)