一个安卓程序,在所有activity中都有一个按钮,可以返回主activity,但是有一个activity出现了不能回到主activity界面,而是单纯的finsh()返回上一个activity了,但是其他的页面的这个按钮都是可以用的。
代码:
public class HelpInfoActivity extends BaseActivity implements OnClickListener{
private ViewFlipper helpImageView;
private float firstposition;
int currentImg = 0;
private static final int FLING_MIN_DISTANCE = 200;
private TextView tvHeadTitle;
private TextView btBack, btMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_helpinfo);
helpImageView= (ViewFlipper)findViewById(R.id.iv_helpinfoimage);
IntentFilter filter = new IntentFilter( );
filter.addAction("android.navi.ok"); //方向盘OK
registerReceiver(mIntentReceiver, filter);
tvHeadTitle=(TextView) findViewById(R.id.tv_shb_title);
tvHeadTitle.setText("使用帮助");
btBack = (TextView) findViewById(R.id.bt_back);
btMap = (TextView) findViewById(R.id.bt_map);
btBack.setOnClickListener(this);
btMap.setOnClickListener(this);
helpImageView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
firstposition = event.getX();
}
if(event.getAction() == MotionEvent.ACTION_UP){
float secondposition = event.getX();
updatePage(secondposition);
}
return true;
}
});
}
private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.navi.ok")) {
if (currentImg++ < 4) {
helpImageView.setInAnimation(inFromRightAnimation());
helpImageView.setOutAnimation(outToLeftAnimation());
helpImageView.showNext();
} else {
finish();
}
}
}
};
@Override
protected void onStop() {
super.onStop();
unregisterReceiver(mIntentReceiver);
}
/** * 定义从右侧进入的动画效果 * @return */
protected Animation inFromRightAnimation() {
Animation inFromRight = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, +1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
inFromRight.setDuration(500);
inFromRight.setInterpolator(new AccelerateInterpolator());
return inFromRight;
}
/** * 定义从左侧退出的动画效果 * @return */
protected Animation outToLeftAnimation() {
Animation outtoLeft = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, -1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
outtoLeft.setDuration(500);
outtoLeft.setInterpolator(new AccelerateInterpolator());
return outtoLeft;
}
/** * 定义从左侧进入的动画效果 * @return */
protected Animation inFromLeftAnimation() {
Animation inFromLeft = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, -1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
inFromLeft.setDuration(500);
inFromLeft.setInterpolator(new AccelerateInterpolator());
return inFromLeft;
}
/** * 定义从右侧退出时的动画效果 * @return */
protected Animation outToRightAnimation() {
Animation outtoRight = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, +1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
outtoRight.setDuration(500);
outtoRight.setInterpolator(new AccelerateInterpolator());
return outtoRight;
}
public void updatePage(float secondposition) {
if (secondposition - firstposition < 0
&& Math.abs(secondposition - firstposition) > FLING_MIN_DISTANCE) {
if (currentImg++ < 4) {
helpImageView.setInAnimation(inFromRightAnimation());
helpImageView.setOutAnimation(outToLeftAnimation());
helpImageView.showNext();
} else {
finish();
}
} else if (secondposition - firstposition > 0
&& Math.abs(secondposition - firstposition) > FLING_MIN_DISTANCE) {
if (currentImg-- > 0) {
helpImageView.setInAnimation(inFromLeftAnimation());
helpImageView.setOutAnimation(outToRightAnimation());
helpImageView.showPrevious();
} else {
currentImg = 0;
}
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.bt_back:
this.finish();
break;
case R.id.bt_map:
this.setResult(ActivityCode.INTEGRATED_GO_MAP);
this.finish();
break;
}
}
}
但是这个activity就可以成功跳转:
public class PayTMCActivity extends FragmentActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pay_tmc);
findViewById(R.id.bt_map).setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
setResult(ActivityCode.INTEGRATED_GO_MAP);
finish();
}
});
findViewById(R.id.bt_back).setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
onBackPressed();
}
});
getSupportFragmentManager().beginTransaction().add(R.id.frame_pay_tmc, new PayTMCMainFragment()).commit();
}
}
是不是返回主Activity,关键看你当前显示的activity下面的activity是不是主activity
比如A打开了B,B打开了C,那么C返回的正常来说就是返回到B,除非你做了Task或者在C关闭的时候主动跳转到A,否则返回不到A这个Activity