Simple Implementation of Android Global Dialog

  • 2021-08-21 21:27:45
  • OfStack

Foreword:

The global Dialog mentioned here means that no matter which page the current application is on, Dialog can pop up in time to prompt the user for some information, and the user experience will not be very good, so it is generally applied to notifications with very high priority.

Analysis:

In fact, it is very simple to realize that Dialog can be popped up in time no matter which page the current application is on, as long as the currently displayed Activity can be obtained, that is, Activity at the top of the stack.

Implementation process: In 1 general project, we will manage our Activity through 1 class, that is, AppManager


/**
 * Activity Management class 
 * @author Donkor
 */
public class AppManager {
  private static Stack<Activity> activityStack;
  private static AppManager instance;

  private AppManager(){}
  /**
   *  Single 1 Instances 
   */
  public static AppManager getAppManager(){
    if(instance==null){
      instance=new AppManager();
    }
    return instance;
  }
  /**
   *  Add Activity To the stack 
   */
  public void addActivity(Activity activity){
    if(activityStack==null){
      activityStack=new Stack<Activity>();
    }
    activityStack.add(activity);
  }
  /**
   *  Gets the current Activity (Last in the stack 1 A pressed-in) 
   */
  public Activity currentActivity(){
    Activity activity=activityStack.lastElement();
    return activity;
  }
  /**
   *  End the current Activity (Last in the stack 1 A pressed-in) 
   */
  public void finishActivity(){
    Activity activity=activityStack.lastElement();
    if(activity!=null){
      activity.finish();
      activity=null;
    }
  }
  /**
   *  Ends the specified Activity
   */
  public void finishActivity(Activity activity){
    if(activity!=null){
      activityStack.remove(activity);
      activity.finish();
      activity=null;
    }
  }
  /**
   *  Object for the specified class name Activity
   */
  public void finishActivity(Class<?> cls){
    for (Activity activity : activityStack) {
      if(activity.getClass().equals(cls) ){
        finishActivity(activity);
      }
    }
  }
  /**
   *  End all Activity
   */
  public void finishAllActivity(){
    for (int i = 0, size = activityStack.size(); i < size; i++){
      if (null != activityStack.get(i)){
        activityStack.get(i).finish();
      }
    }
    activityStack.clear();
  }
  /**
   *  Exit the application 
   */
  public void AppExit(Context context) {
    try {
      finishAllActivity();
      ActivityManager activityMgr= (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
      activityMgr.restartPackage(context.getPackageName());
      System.exit(0);
    } catch (Exception e) { 
      e.printStackTrace();
    }
  }
}

Then encapsulate one BaseActivity class system 1 management and inherit it to each Activity


/**
 * Activity Base class 
 * @author Donkor
 */
public abstract class BaseActivity extends AppCompatActivity {

  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Will Activity Instance is added to the AppManager Stack of 
    AppManager.getAppManager().addActivity(this);
  }


  @Override
  protected void onDestroy() {
    super.onDestroy();
    // Will Activity Instance from the AppManager Remove from the stack of 
    AppManager.getAppManager().finishActivity(this);
  }

}

Finally, it is necessary to deal with the display of Dialog in MainActivity (main page, main class) or Service, and get Activity at the top of the current stack through AppManager for constructing Dialog.


Dialog myDialog = new Dialog(AppManager.getAppManager().currentActivity(), R.style.dialog_style); '


Related articles: