我想在更改主题后对活动重新创建()进行转换,是否可能?
我试过了: @android:动画/ fade_in @android:动画/ fade_out 但它不起作用,当我打开和关闭活动时,这也会影响过渡,但我不想那样
答案 0 :(得分:0)
嗯,你可以使用它而不是重新创建()
Activity mCurrentActivity = getActivity();
...
mCurrentActivity.finish();
mCurrentActivity.overridePendingTransition(R.anim.transition_for_incoming_activity, R.anim.transition_for_outgoing_activity);
mCurrentActivity.startActivity(mCurrentActivity.getIntent());
答案 1 :(得分:0)
为了使用@Arunava的答案“保存状态”,请执行此操作
Activity mCurrentActivity = getActivity();
Intent intent = getActivity().getIntent();
Bundle tempBundle = new Bundle();
intent.putExtra("bundle", tempBundle);
mCurrentActivity.finish();
mCurrentActivity.overridePendingTransition(R.anim.transition_for_incoming_activity, R.anim.transition_for_outgoing_activity);
mCurrentActivity.startActivity(intent);
,然后在您活动的onCreate中完成
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getIntent().hasExtra("bundle")){
//Insert a method to display the activity or fragment that triggered the activity to restart
}
super.onResume();
}
答案 2 :(得分:0)
完成@Yaro的答案,
在onCreate
内,如果saveInstanceState为null,请尝试其他目的。仅当您使用捆绑包调用super.onCreate
时,视图状态才能正确恢复。
public class ExampleActivity extends Activity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
//setTheme(whatever);
super.onCreate(savedInstanceState != null ? savedInstanceState : getIntent().getBundleExtra("saved_state"));
}
protected void transitionRecreate(){
Bundle bundle = new Bundle();
onSaveInstanceState(bundle);
Intent intent = new Intent(this, getClass());
intent.putExtra("saved_state", bundle);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
}
}
为我工作,您可以使用finish()
代替CLEAR_TOP
标志