android 返回到首页案例

很多年前做Android开发,经常遇到页面跳转需要返回到指定页面的需求。现再次总结下:

一、ActivityA返回到ActivityB

ActivityA返回到ActivityB,回收掉中间页面

 Intent intent = new Intent(ActivityA.this,ActivityB.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        startActivity(intent);

二、逐级返回

在除首页外的每一个页面重写onActivityResult方法:

/**
 * 页面退出回调
 * Author:William(徐威)
 * Create Time:2018-07-31
 *
 * @param requestCode
 * @param resultCode
 * @param data
 */
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == 0 && resultCode == RESULT_OK) {
        setResult(RESULT_OK);
        super.onDestroy();
        this.finish();
    }
}

在要返回的页面调用代码:

Intent intent = new Intent(ActivityA.this, ActivityB.class);
setResult(RESULT_OK, intent);

注意:这里的ActivityB是指当前活动的上一层,写好之后,会自动层层返回至首页。

三、页面管理工具

    ActivityPageManager

**
 * Activity管理类
 * Created by Administrator on 2016/11/24.
 */
public class ActivityPageManager {
  private static Stack<Activity> activityStack;
  private static ActivityPageManager instance;
  /**
   * constructor
   */
  private ActivityPageManager() {
  }
  /**
   * get the AppManager instance, the AppManager is singleton.
   */
  public static ActivityPageManager getInstance() {
    if (instance == null) {
      instance = new ActivityPageManager();
    }
    return instance;
  }
  /**
   * add Activity to Stack
   */
  public void addActivity(Activity activity) {
    if (activityStack == null) {
      activityStack = new Stack<Activity>();
    }
    activityStack.add(activity);
  }
  /**
   * remove Activity from Stack
   */
  public void removeActivity(Activity activity) {
    if (activityStack == null) {
      activityStack = new Stack<Activity>();
    }
    activityStack.remove(activity);
  }
  /**
   * get current activity from Stack
   */
  public Activity currentActivity() {
    Activity activity = activityStack.lastElement();
    return activity;
  }
  /**
   * finish current activity from Stack
   */
  public void finishActivity() {
    Activity activity = activityStack.lastElement();
    finishActivity(activity);
  }
  /**
   * finish the Activity
   */
  public void finishActivity(Activity activity) {
    if (activity != null) {
      activityStack.remove(activity);
      activity.finish();
      activity = null;
    }
  }
  /**
   * finish the Activity
   */
  public void finishActivity(Class<?> cls) {
    for (Activity activity : activityStack) {
      if (activity.getClass().equals(cls)) {
        finishActivity(activity);
      }
    }
  }
  /**
   * finish all Activity
   */
  public void finishAllActivity() {
    if(activityStack!=null&&activityStack.size()>0)
    {
      for (int i = 0, size = activityStack.size(); i < size; i++) {
        if (null != activityStack.get(i)) {
          activityStack.get(i).finish();
        }
      }
      activityStack.clear();
    }
  }
  /**
   * release all resourse for view
   * @param view
   */
  public static void unbindReferences(View view) {
    try {
      if (view != null) {
        view.destroyDrawingCache();
        unbindViewReferences(view);
        if (view instanceof ViewGroup){
          unbindViewGroupReferences((ViewGroup) view);
        }
      }
    } catch (Throwable e) {
      // whatever exception is thrown just ignore it because a crash is
      // always worse than this method not doing what it's supposed to do
    }
  }
  private static void unbindViewGroupReferences(ViewGroup viewGroup) {
    int nrOfChildren = viewGroup.getChildCount();
    for (int i = 0; i < nrOfChildren; i++) {
      View view = viewGroup.getChildAt(i);
      unbindViewReferences(view);
      if (view instanceof ViewGroup)
        unbindViewGroupReferences((ViewGroup) view);
    }
    try {
      viewGroup.removeAllViews();
    } catch (Throwable mayHappen) {
      // AdapterViews, ListViews and potentially other ViewGroups don't
      // support the removeAllViews operation
    }
  }
  @SuppressWarnings("deprecation")
  private static void unbindViewReferences(View view) {
    // set all listeners to null (not every view and not every API level
    // supports the methods)
    try {
      view.setOnClickListener(null);
      view.setOnCreateContextMenuListener(null);
      view.setOnFocusChangeListener(null);
      view.setOnKeyListener(null);
      view.setOnLongClickListener(null);
      view.setOnClickListener(null);
    } catch (Throwable mayHappen) {
    }
    // set background to null
    Drawable d = view.getBackground();
    if (d != null){
      d.setCallback(null);
    }
    if (view instanceof ImageView) {
      ImageView imageView = (ImageView) view;
      d = imageView.getDrawable();
      if (d != null){
        d.setCallback(null);
      }
      imageView.setImageDrawable(null);
      imageView.setBackgroundDrawable(null);
    }
    // destroy WebView
    if (view instanceof WebView) {
      WebView webview = (WebView) view;
      webview.stopLoading();
      webview.clearFormData();
      webview.clearDisappearingChildren();
      webview.setWebChromeClient(null);
      webview.setWebViewClient(null);
      webview.destroyDrawingCache();
      webview.destroy();
      webview = null;
    }
    if (view instanceof ListView) {
      ListView listView = (ListView) view;
      try {
        listView.removeAllViewsInLayout();
      } catch (Throwable mayHappen) {
      }
      ((ListView) view).destroyDrawingCache();
    }
  }
  /**
   * exit System
   * @param context
   */
  public void exit(Context context) {
    exit(context, true);
  }
  /**
   * exit System
   * @param context
   * @param isClearCache
   */
  @SuppressWarnings("deprecation")
  public void exit(Context context, boolean isClearCache) {
    try {
      finishAllActivity();
      if(context != null){
        ActivityManager activityMgr = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        activityMgr.restartPackage(context.getPackageName());
      }
//      if(isClearCache){
//        LruCacheManager.getInstance().evictAll();
//        CacheManager.clearAll();
//      }
      System.exit(0);
      android.os.Process.killProcess(android.os.Process.myPid());
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

返回直接调用 finishAllActivity

  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值