Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android make transition on activity recreate()

Tags:

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

like image 727
4face Avatar asked Feb 08 '17 18:02

4face


People also ask

How do you recreate an activity?

For recreating the activity, you can use the simple recreate() or startActivity(intent). or can use to reopen the activty by startActivity.

When activity is recreated?

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.


Video Answer


1 Answers

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

like image 145
chevreto Avatar answered Sep 28 '22 05:09

chevreto