我想知道如果退出当前活动,用户是否会返回主屏幕.
我有一个MainActivity(它有launchMode = singleTop),我可以从中进行其他活动,例如B和C.现在,我想回到MainActivity一些按钮点击B和C.而且我也想要过渡动画.
这是代码
CODE 1
Intent intent=new Intent(this,MainActivity.class);
Bundle animation= ActivityOptions.makeCustomAnimation(getApplicationContext(), R.animator.translate_left_to_right, R.animator.translate_source_left_to_right).toBundle();
startActivity(intent,animation);
finish();
Run Code Online (Sandbox Code Playgroud)
上面的代码工作正常,除了MainActivity在旧的代码之上创建一个新实例的事实!我不希望这种情况发生.因此,经过一些研究后我尝试了下面的代码
CODE 2
Intent intent=new Intent(this,ListingActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Bundle animation= ActivityOptions.makeCustomAnimation(getApplicationContext(), R.animator.translate_left_to_right, R.animator.translate_source_left_to_right).toBundle();
startActivity(intent,animation);
finish();
Run Code Online (Sandbox Code Playgroud)
此代码似乎消除了在标志FLAG_ACTIVITY_CLEAR_TOP处理它时创建活动的新实例的问题.但是,现在过渡动画似乎不起作用!
旗帜FLAG_ACTIVITY_CLEAR_TOP不允许任何动画吗?我的问题有什么解决方案?我需要动画转换以及MainActivity不应该创建新的实例.
编辑
这似乎是David Wasser建议的伎俩.
Intent intent=new Intent(this,ListingActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
finish();
overridePendingTransition(R.animator.translate_left_to_right,R.animator.translate_source_left_to_right);
Run Code Online (Sandbox Code Playgroud)
但动画不流畅.动画中有一个小故障.我认为那是因为在动画完成之前活动(B或C)被破坏了.我不确定
发布动画文件
translate_left_to_right.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="-100%p"
android:toXDelta="0%p"
android:duration="400"/>
Run Code Online (Sandbox Code Playgroud)
translate_right_to_left.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android" …Run Code Online (Sandbox Code Playgroud)