后退按钮上的自定义动画 - 活动

时间:2014-11-02 09:50:58

标签: android animation transition

如您所见,我为菜单中的每个按钮添加了一个转换,现在我想为后退按钮添加淡入/淡出转换。 因此,当我按下该按钮时,活动将关闭播放该过渡。 我该怎么做?

        @Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        Toast.makeText(MainActivity.this, "Settings selected", Toast.LENGTH_SHORT).show();
    }

    if(id == R.id.action_settings) {
        Intent openAboutActivityIntent = new Intent(this, third_activity.class);
        startActivity(openAboutActivityIntent);
        overridePendingTransition(R.anim.push_down_in,R.anim.push_down_out);

    } else if (id == R.id.Exit){
        finish();
        overridePendingTransition(R.anim.fade_in,R.anim.fade_out);
        return true;

    }switch (item.getItemId()) {

        case R.id.About:
           Toast.makeText(MainActivity.this, "About selected", Toast.LENGTH_SHORT).show();
    }
      if(id == R.id.About) {
            Intent openAboutActivityIntent = new Intent(this, second_activity.class);
            startActivity(openAboutActivityIntent);
            overridePendingTransition(R.anim.push_down_in,R.anim.push_down_out);
    }

    return super.onOptionsItemSelected(item);
}

@Override
public void onBackPressed() {
    Log.d("CDA", "onBackPressed Called");
    Intent setIntent = new Intent(Intent.ACTION_MAIN);
    setIntent.addCategory(Intent.CATEGORY_HOME);
    setIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(setIntent);
}

}

1 个答案:

答案 0 :(得分:1)

@Override
public void onBackPressed() {
    //other code
    overridePendingTransition(R.anim.push_down_in,R.anim.push_down_out);
}