Android
想从任意activity跳出方法。
在前一个activity跳转后加上finish();
然后判断if(isfinish()){onDestroy();}
代码如下:
OneTestActivity
public class OneTestActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (isFinishing()){
onDestroy();
}
setContentView(R.layout.activity_one);
findView();
initView();
}
private void findView() {
}
private void initView() {
startActivity(new Intent(OneTestActivity.this,TwoTestActivity.class));
finish();
}
@Override
protected void onDestroy() {
finish();
super.onDestroy();
}
}
TwoTestActivity
public class TwoTestActivity extends Activity {
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
findView();
initView();
}
private void findView() {
btn= (Button) findViewById(R.id.back_btn);
}
private void initView() {
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
}
}
这样 finish时直接就从第二个Activity出去了。