Android Activity Start openActivity(Context packageContext, int flags, Map pairs, boolean closeCurrentContext, Class activity)

Here you can find the source of openActivity(Context packageContext, int flags, Map pairs, boolean closeCurrentContext, Class activity)

Description

Purpose - Launch an activity

Parameter

Parameter Description
packageContext - Current context
flags - Intent flags to launch with the activity
pairs - parameters to the activity as a Bundle
closeCurrentContext - whether or not to close the current activity\service
activity - Activity to launch

Declaration

public static void openActivity(Context packageContext, int flags,
        Map<String, String> pairs, boolean closeCurrentContext,
        Class<?> activity) 

Method Source Code

//package com.java2s;

import java.util.Map;
import java.util.Map.Entry;
import android.app.Activity;
import android.app.Service;
import android.content.Context;
import android.content.Intent;

import android.os.Bundle;

public class Main {
    /**/*from  w ww . j a va 2s .  com*/
     * Purpose - Launch an activity
     * 
     *  @param packageContext - Current context
     *  @param flags - Intent flags to launch with the activity
     *  @param pairs - parameters to the activity as a Bundle
     *  @param closeCurrentContext - whether or not to close the current activity\service
     *  @param activity - Activity to launch
     * 
     * */
    public static void openActivity(Context packageContext, int flags,
            Map<String, String> pairs, boolean closeCurrentContext,
            Class<?> activity) {
        if (packageContext == null)
            return;
        Intent intent = new Intent(packageContext, activity);
        //set the intent flags if any
        intent.setFlags(flags);
        if (pairs != null && !pairs.isEmpty()) {
            Bundle extras = new Bundle();
            for (Entry<String, String> pair : pairs.entrySet()) {
                extras.putString(pair.getKey(), pair.getValue());
            }
            intent.putExtras(extras);
        }
        //start the activity
        packageContext.startActivity(intent);
        //Close the current context
        if (closeCurrentContext) {
            if (packageContext instanceof Activity) {
                ((Activity) packageContext).finish();
            } else if (packageContext instanceof Service) {
                ((Service) packageContext).stopSelf();
            }
        }
    }
}

Related

  1. goToActivity(Context currentActivity, Class newClass)
  2. goToActivity(Context currentActivity, Class newClass, List extras)
  3. startApkActivity(final Context ctx, String packageName)
  4. restartActivity(final Activity activity)
  5. restartPC(final Activity activity)
  6. startActivity(Context context, Class activity)