Execute code before executing the first Activity

Asked

Viewed 487 times

2

I have the following problem: I have a splash screen which will be called only if it is the first execution of the application. If it’s not the first execution I’d like to call another Activity.

I tried the following... to make a InitialActivity just like 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 this guy calls the super.onCreate and ends up creating this Activity. Turns out it shows a white screen before deciding which Activity initial i want to go.

How can I solve this problem?

3 answers

5

You can finish the Initialactivity right after calling the Mainactivity doing so:

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

Doing so, your Activity will be closed before being displayed as it will only be displayed when onStart() is called, which will not happen if you execute finish() before.

  • This will not disrupt code execution after onCreate?

  • It won’t. The code will be executed normally and after the onCreate() the onDestroy() will be executed.

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 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 property: name at the node on Androidmanifest.

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

This is all you need to start using your custom app.

  • Welcome to Stackoverflow. Your reply is a good suggestion, but it would be better to include more details in your reply, an example of the code would help a lot!

  • 1

    @Lucas Duete edited post. More references

  • 1

    https://developer.android.com/reference/android/app/Application

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 guy to the manifesto:

android:theme="@style/Theme.Transparent"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.