zoukankan      html  css  js  c++  java
  • Android 在Activity中关闭另一个Activity

    比如现在有N个activity a b c d ....
    如果在d上关闭activity a

    An activity can be shut down by calling its finish() method. One activity can shut down another activity (one it started with startActivityForResult()) by calling finishActivity().


    在a里面设置一个静态的变量instance,初始化为this
    在D里面,a.instance.finish()

    如下面的例子:

    1.对于简单的两个Activity

    public class A_activity extends Activity {
    
    public static A_activity _instance = null;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    .....
    _instance = this;
    }
    
    }
    
    public class B_activity extends Activity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    A_activity._instance.finish(); 
    }
    
    }

    2.也可以通过ActivityManager

    ActivityManager manager = (ActivityManager)getSystemService(ACTIVITY_SERVICE);
    manager.restartPackage(packageName);

    第一个我已经实践过了,很好用,第二种方法不太理解。

  • 相关阅读:
    Mysql优化之Explain查询计划查看
    map转listmap
    代码大全
    cas
    日志
    xml模板
    springboot
    日志
    spring应用
    拆分表中sheet
  • 原文地址:https://www.cnblogs.com/xuewater/p/2659312.html
Copyright © 2011-2022 走看看