比如有ActivityA, ActivityB,在ActivityB中关闭ActivityA
解决方案:
1.
在 ActivityA 里面设置一个静态的变量instance,初始化为this
在 ActivityB 里面, ActivityA.instance.finish();
2.
也可以通过ActivityManager
ActivityManager manager = (ActivityManager)getSystemService(ACTIVITY_SERVICE);
manager.restartPackage(packageName);
可以加入:Android 开发总交流群:179730949(500人) 或 Android开发总交流群:249919939(1000人)
例子如下:
ActivityA
package com.activity.yuzhenbei;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
public static ActivityA instance = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activitya);
instance = this;
Intent intent = new Intent();
intent.setClass(ActivityA.this, ActivityB.class);
ActivityA.this.startActivity(intent);
}
// Menu
// 当点击Menu按钮时,调用该方法
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, 1, 1, R.string.help).setIcon(
android.R.drawable.ic_menu_close_clear_cancel);
return super.onCreateOptionsMenu(menu);
}
// 选中某个菜
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == 1) {
Intent intent = new Intent();
intent.setClass(ActivityA.this, ActivityB.class);
ActivityA.this.startActivity(intent);
}
return super.onOptionsItemSelected(item);
}
// 返回键
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) { // 如果是手机上的返回键
ActivityA.this.finish();
}
return super.onKeyDown(keyCode, event);
}
}
ActivityB
package com.activity.yuzhenbei;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
public class ActivityB extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activityb);
}
// 返回键
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) { // 如果是手机上的返回键
ActivityB.this.finish();
ActivityA.instance.finish();
}
return super.onKeyDown(keyCode, event);
}
}
http://www.baidu.com/link?url=QlRuDIKVe0nDyagyUJOnDp8PDau_OJWmBX1nLq6gC_G5Kzdlg-lfqAVcdlw5C5Luceiu4ZE1gASnnRYy3VJ4Ma