Android How to - Navigate to Activity








Question

We would like to know how to navigate to Activity.

Answer

//  w w  w. j  a v a  2s . co  m
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;

class ActivityHelper {

  public static void navigateTo(Activity activity, final Fragment fragment,
      int containerId) {
    navigateTo(activity, fragment, null, true, false, false, containerId);
  }

  public static void navigateTo(Activity activity, final Fragment fragment,
      final boolean addToBackStack, int containerId) {
    navigateTo(activity, fragment, null, addToBackStack, false, false,
        containerId);
  }

  public static void navigateTo(Activity activity, final Fragment fragment,
      final FragmentTransaction transaction, final boolean addToBackStack,
      final boolean allowStateLoss, final boolean useTransition, int containerId) {
    final FragmentManager fm = activity.getFragmentManager();
    final FragmentTransaction ft = transaction == null ? fm.beginTransaction()
        : transaction;
    ft.setTransition(useTransition ? FragmentTransaction.TRANSIT_FRAGMENT_OPEN
        : FragmentTransaction.TRANSIT_NONE);

    ft.replace(containerId, fragment, fragment.getClass().getName());
    if (addToBackStack) {
      ft.addToBackStack(fragment.getClass().getName());
    }

    commitTransaction(ft, allowStateLoss);
  }

  private static void commitTransaction(final FragmentTransaction ft,
      final boolean allowStateLoss) {
    if (allowStateLoss) {
      ft.commitAllowingStateLoss();
    } else {
      ft.commit();
    }
  }

  private static void closeActivity(Activity activity) {
    Intent intent = activity.getIntent();
    activity.setResult(Activity.RESULT_OK, intent);
    activity.finish();
  }
}