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);

 

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

 

你可能感兴趣的:(Activity)