虽然如今都用Fragment了,但做为Android新手,我遇到一个须要Activity之间切换而不销毁以前的Activity的问题。ide
Activity A->B->C 而后返回, C->B->A 。 再A->B->C动画
这时,我不想C和B被销毁,我不在意堆栈结构,但不能让C和B从新onCreate(),而只是调用onNewIntent()。this
搜索了不少,最后仍是在外网找到方法。C->B和B->A时不用finish(),而是从新new Intent,并且调用startActivity()前,component
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
根据Google本身的解释:Intent.FLAG_ACTIVITY_REORDER_TO_FRONTxml
* <p>For example, consider a task consisting of four activities: A, B, C, D. * If D calls startActivity() with an Intent that resolves to the component * of activity B, then B will be brought to the front of the history stack, * with this resulting order: A, C, D, B.
另外注意get
AndroidManifest.xml里的Activity的launchMode必定都去掉。it
这时A,B,C已经没有什么堆栈关系了,不管前进,后退,都是startActivity()。io
这时动画会有点问题,push,pop都是从push那种动画,从右边进入,左边退出。class
这时须要用到自定义动画,不过返回的自定义动画会发现无效,解决方法是overridePendingTransition写到onNewIntent里面:搜索
好比B的出现动画包括A->B和C->B,能够根据"from"来判断
protected void onNewIntent(Intent intent) { super.onNewIntent(intent); String from = intent.getStringExtra("from"); if (from != null && from.equals("result")) { overridePendingTransition(R.anim.in_from_left, R.anim.out_to_right); } else { overridePendingTransition(R.anim.in_from_rigt, R.anim.out_to_left); }