The fastest way to change the theme when starting the application

I currently provide 2 themes in my application, based on the user's last choice - a dark theme and a light theme.

During the start of the main activity, I will do the following.

public class MyFragmentActivity extends FragmentActivity { @Override public void onCreate(Bundle savedInstanceState) { // getUserLastThemeChoice will either return R.style.Theme_My_Light or // R.style.Theme_My_Dark. this.setTheme(getUserLastThemeChoice()); super.onCreate(savedInstanceState); 

In my AndroidManifest.xml , I have the following

 <application ... android:theme="@style/Theme.My.Dark" > 

Due to the hard-coded value in AndroidManifest.xml , if the user's previous choice is an easy topic, I will observe the following.

  • The application starts with a dark theme, due to AndroidManifest.xml
  • A while ...
  • The app will change to a white theme due to this.setTheme in the main Activity

Anyway, can I avoid such a transient observation? What it is is simply representing the user directly, based on their last choice.

p / s Note. This will not help if you remove topic information from the manifest. Since the system will use the hologram dark by default. You will still see the transition, especially in a slow device.

+6
source share
2 answers

Hiding the manipulation of the theme, especially when starting the application, is difficult. Unfortunately, there is no way to remove this initial transition sequentially, because it is not known how fast / slow its device works. Android will always show this empty background in the default theme before inflating the layout for the main action.

But there is a trick to hide it. A screensaver that appears when the user opens the application and disappears after loading the initial activity. Thus, the splash screen hides the transition to the topic without annoying the user. Pop-up screens get some well-deserved hatred, but this situation justifies their use. Again, I do not know the specifics of your application, but you need to weigh the pros and cons of having a splash screen and a smooth transition, without a splash in the screen and a sudden transition.

There are many ways to implement a screensaver. In your case, I would try to create a launch activity with a neutral theme and apply smooth animation to transition between the splash screen and your main activity as follows:

 startActivity( new Intent( LauncherActivity.this, MainActivity.class ) ); overridePendingTransition( R.anim.fade_in, R.anim.fade_out ); 

And animations:

 <!-- fadeout --> <?xml version="1.0" encoding="UTF-8"?> <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator" android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="300" /> <!-- fadein --> <?xml version="1.0" encoding="utf-8"?> <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator" android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="300" /> 
+1
source

I know this is very old, but maybe it still helps:

If you set an activity topic

 android:theme="@android:style/Theme.Translucent.NoTitleBar" 

he will not show this initial screen. Instead, you just need a little more so that the application does not show anything at all.

0
source

All Articles