Skip to content

Instantly share code, notes, and snippets.

@kiuchikeisuke
Last active July 23, 2018 23:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kiuchikeisuke/e77fac405495ed5ccfa1ae3ba166170a to your computer and use it in GitHub Desktop.
Save kiuchikeisuke/e77fac405495ed5ccfa1ae3ba166170a to your computer and use it in GitHub Desktop.
JetpackのNavigationのオプション、NavOptionsをざっくり覗いてみる ref: https://qiita.com/k_keisuke/items/29cf9f4df7b3cdcbb147
/**
* 略
* @param launchDocument true to launch a new document task
* @deprecated As per the {@link android.content.Intent#FLAG_ACTIVITY_NEW_DOCUMENT}
* documentation, it is recommended to use {@link android.R.attr#documentLaunchMode} on an
* Activity you wish to launch as a new document.
*/
@Deprecated
@NonNull
public Builder setLaunchDocument(boolean launchDocument) {/* 略 */}
@SuppressWarnings("deprecation")
@Override
public void navigate(@NonNull Destination destination, @Nullable Bundle args,
@Nullable NavOptions navOptions) {
/* 略 */
NavOptions.addPopAnimationsToIntent(intent, navOptions);
/* 略 */
}
view?.findViewById<Button>(R.id.button_to_activity)?.setOnClickListener {
val options = NavOptions.Builder()
.setPopEnterAnim(R.anim.abc_fade_in)
.setPopExitAnim(R.anim.abc_fade_out)
.build()
Navigation.findNavController(it).navigate(R.id.action_mainFragment_to_main2Activity, null, options)
}
view?.findViewById<Button>(R.id.button_to_activity)?.setOnClickListener {
val options = NavOptions.Builder().setClearTask(true).build()
Navigation.findNavController(it).navigate(R.id.action_mainFragment_to_main2Activity, null, options)
}
<activity android:name=".Main2Activity"
android:documentLaunchMode="always">
</activity>
val intent = Intent(context, SomeActivity::class.java)
intent.flags = Intent.LAUNCH_CLEAR_TASK
context.startActivity(intent)
public void navigate(@IdRes int resId, @Nullable Bundle args, @Nullable NavOptions navOptions) {
/* 略 */
if (destId == 0 && navOptions != null && navOptions.getPopUpTo() != 0) {
popBackStack(navOptions.getPopUpTo(), navOptions.isPopUpToInclusive());
return;
}
/* 略 */
}
/**
* Attempts to pop the controller's back stack back to a specific destination.
*
* @param destinationId The topmost destination to retain
* @param inclusive Whether the given destination should also be popped.
*
* @return true if the stack was popped at least once, false otherwise
*/
public boolean popBackStack(@IdRes int destinationId, boolean inclusive) {
if (mBackStack.isEmpty()) {
throw new IllegalArgumentException("NavController back stack is empty");
}
ArrayList<NavDestination> destinationsToRemove = new ArrayList<>();
Iterator<NavDestination> iterator = mBackStack.descendingIterator();
while (iterator.hasNext()) {
NavDestination destination = iterator.next();
if (inclusive || destination.getId() != destinationId) {
destinationsToRemove.add(destination);
}
if (destination.getId() == destinationId) {
break;
}
}
boolean popped = false;
iterator = destinationsToRemove.iterator();
while (iterator.hasNext()) {
NavDestination destination = iterator.next();
// Skip destinations already removed by a previous popBackStack operation
while (!mBackStack.isEmpty() && mBackStack.peekLast().getId() != destination.getId()) {
if (iterator.hasNext()) {
destination = iterator.next();
} else {
destination = null;
break;
}
}
if (destination != null) {
popped = destination.getNavigator().popBackStack() || popped;
}
}
return popped;
}
/**
* Add the {@link #getPopEnterAnim() pop enter} and {@link #getPopExitAnim() pop exit}
* animation to an Intent for later usage with
* {@link #applyPopAnimationsToPendingTransition(Activity)}.
* <p>
* This is automatically called for you by {@link ActivityNavigator}.
* </p>
*
* @param intent Intent being started with the given NavOptions
* @param navOptions NavOptions containing the pop animations.
* @see #applyPopAnimationsToPendingTransition(Activity)
* @see #getPopEnterAnim()
* @see #getPopExitAnim()
*/
public static void addPopAnimationsToIntent(@NonNull Intent intent,
@Nullable NavOptions navOptions) {
if (navOptions != null) {
intent.putExtra(KEY_NAV_OPTIONS, navOptions.toBundle());
}
}
/**
* Apply any pop animations in the Intent of the given Activity to a pending transition.
* This should be used in place of {@link Activity#overridePendingTransition(int, int)}
* to get the appropriate pop animations.
* @param activity An activity started from the {@link ActivityNavigator}.
* @see #addPopAnimationsToIntent(Intent, NavOptions)
* @see #getPopEnterAnim()
* @see #getPopExitAnim()
*/
public static void applyPopAnimationsToPendingTransition(@NonNull Activity activity) {
Intent intent = activity.getIntent();
if (intent == null) {
return;
}
Bundle bundle = intent.getBundleExtra(KEY_NAV_OPTIONS);
if (bundle != null) {
NavOptions navOptions = NavOptions.fromBundle(bundle);
int popEnterAnim = navOptions.getPopEnterAnim();
int popExitAnim = navOptions.getPopExitAnim();
if (popEnterAnim != -1 || popExitAnim != -1) {
popEnterAnim = popEnterAnim != -1 ? popEnterAnim : 0;
popExitAnim = popExitAnim != -1 ? popExitAnim : 0;
activity.overridePendingTransition(popEnterAnim, popExitAnim);
}
}
}
val intent = Intent(context, SomeActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK
context.startActivity(intent)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment