Android How to - Switch Activity








Question

We would like to know how to switch Activity.

Answer

// w w w  .ja v a  2 s. c  o m
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

public class Main {
  public static void jump(Context old, Class<?> cls, int requestCode,Bundle mBundle,boolean clearTop){
       Intent intent = new Intent();  
        intent.setClass(old, cls);
        if(mBundle == null){
          mBundle = new Bundle();
          
        }   
        Activity oldActivity = (Activity) old;
        mBundle.putString("from", oldActivity.getClass().getSimpleName());
        intent.putExtras(mBundle);
      
        Activity activity = (Activity) old;
        if(clearTop){
          intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
          activity.finish();
        }
        activity.startActivityForResult(intent, requestCode); 
  }
}