AndroidはActivityで別のActivityを閉じます

2150 ワード

例えば今は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.単純な2つの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.Activity Managerから
 
ActivityManager manager = (ActivityManager)getSystemService(ACTIVITY_SERVICE); manager.restartPackage(packageName);
 
一つ目はもう実践しましたが、使いやすくて、二つ目の方法はあまり理解できません.