Android make transition on activity recreate()
Asked Answered
V

4

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

Vineland answered 8/2, 2017 at 18:3 Comment(0)
W
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

Weinreb answered 8/8, 2020 at 9:8 Comment(0)
C
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();
}
Cupric answered 24/9, 2019 at 17:36 Comment(0)
P
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 ..

Pounce answered 5/8, 2023 at 12:19 Comment(0)
L
-3

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());
Logic answered 20/2, 2017 at 3:47 Comment(1)
this will not save instanceStateRecoil

© 2022 - 2024 — McMap. All rights reserved.