Run code before the first activity is executed

2

I have the following problem: I have a splash screen that will be called only if it is the first application execution. If it is not the first run I would like to call another Activity .

I tried the following ... make a InitialActivity as below:

public class InitialActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_initial);
        final SharedPreferences sharedPref = getPreferences(MODE_PRIVATE);
        boolean isFirstUse = sharedPref.getBoolean("is_first_use", true);

        if (isFirstUse)
        {
            SharedPreferences.Editor editor = sharedPref.edit();
            editor.putBoolean("is_first_use", false);
            editor.commit();
            startActivity(new Intent(this, SplashScreen.class));
        } else {
            //verify mode and call correct activity
            startActivity(new Intent(this, MainActivity.class));
        }
    }
}

The problem is that this guy calls super.onCreate and ends up creating this Activity . It turns out that it shows a white screen before deciding which initial Activity I want to go to.

How can I solve this problem?

    
asked by anonymous 16.01.2015 / 18:16

3 answers

1

First I created a theme:

<style name="Theme.Transparent" parent="android:Theme">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:backgroundDimEnabled">false</item>
</style>

Then I added this face to the manifest:

android:theme="@style/Theme.Transparent"
    
16.01.2015 / 19:14
5

You can end InitialActivity shortly after calling MainActivity by doing this:

startAcitivity(new Intent(this, MainActivity.class));
finish();

By doing so, your Activity will be closed before it is displayed, because it will only be displayed when onStart() is called, which will not happen if you run finish() before.

    
16.01.2015 / 18:34
1

You can control this by creating a class that inherits from the Application class, and in it you control a variable that will be saved in the preferences.

Defining your application class

If we want a custom application class, we start by creating a new class that extends android.app.Application as follows:

public class MyApplication extends Application {

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
    }

}

And specify the android: name property on the node in AndroidManifest.

<application 
   android:name=".MyCustomApplication"
   android:icon="@drawable/icon" 
   android:label="@string/app_name" 
   ...>

That's all you need to get started with your custom application.

    
13.10.2018 / 23:34