I would like to put a transition on activity recreate() after changing theme, is it possible?
I tried: @android:anim/fade_in @android:anim/fade_out but it didn't work, and that will also affect the transition when I open and close activity, but I don't want that
For recreating the activity, you can use the simple recreate() or startActivity(intent). or can use to reopen the activty by startActivity.
Restore Your Activity State When your activity is recreated after it was previously destroyed, you can recover your saved state from the Bundle that the system passes your activity. Both the onCreate() and onRestoreInstanceState() callback methods receive the same Bundle that contains the instance state information.
Completing @Yaro's answer,
Inside onCreate
, if savedInstanceState is null, try the intent extras. The state of the views will be properly restored only if you call super.onCreate
with a bundle.
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); } }
Worked for me, you can use finish()
instead of the CLEAR_TOP
flag
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With