25

我想在更改主题后对活动 recreate() 进行转换,可以吗?

我试过:@android:anim/fade_in @android:anim/fade_out 但它不起作用,当我打开和关闭活动时,这也会影响过渡,但我不希望那样

4

3 回答 3

1

完成@Yaro的回答,

在内部onCreate,如果 savedInstanceState 为 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标志

于 2020-08-08T09:08:20.590 回答
1

为了使用@Arunava's answer“保存状态”,请执行此操作

    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);

然后在 Activity 的 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();
}
于 2019-09-24T17:36:00.073 回答
-2

那么你可以使用它而不是 recreate()

Activity mCurrentActivity = getActivity();
...
mCurrentActivity.finish();
mCurrentActivity.overridePendingTransition(R.anim.transition_for_incoming_activity, R.anim.transition_for_outgoing_activity);
mCurrentActivity.startActivity(mCurrentActivity.getIntent());
于 2017-02-20T03:47:26.400 回答