26

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

4face
  • 590
  • 1
  • 8
  • 18

4 Answers4

2

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

chevreto
  • 234
  • 1
  • 4
  • 7
1

In order to "save state" using @Arunava's answer, Do this

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

and then do this in your Activity's 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();
}
Yaro
  • 11
  • 4
0

I actually just found that recreate is ignoring animations set in theme so I need to do it just manually:

class SomeActivity:AppcompatActivity(){
...
    override fun recreate() {
        finish()
        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)
        application.startActivity(Intent<InstrumentsActivity>(this))
        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)
    }
...
}

PS: You need to call startActivity on application context for transition to be there. So instead of calling it elswere I just override recreate in activity subclass I already use.

I dont use savedInstanceState, so no need to deal with anything else..

Edit: So in the end I added also those annimation overrides as this was behaving differently on older android version.. (api 26) Added for finish as well as start as this should be places after the overriden call ..

Renetik
  • 5,887
  • 1
  • 47
  • 66
-2

Well you could use this instead of recreate()

Activity mCurrentActivity = getActivity();
...
mCurrentActivity.finish();
mCurrentActivity.overridePendingTransition(R.anim.transition_for_incoming_activity, R.anim.transition_for_outgoing_activity);
mCurrentActivity.startActivity(mCurrentActivity.getIntent());
Arunava
  • 353
  • 1
  • 4
  • 13