aselims
aselims

Reputation: 1893

pre main activity execution "initialization"

The question can be asked in two ways: - How I can make some processing of variables and data before launching the main activity? - How I set the main launcher activity based on some logic? i.e. before viewing an activity from a set of activities, I should retrieve data from preferences. And this should be done only for the 1st usage without preferences activity to be saved in the back button stack.

    public static String getProfile(Context context) {
    SharedPreferences mSharedPreferences =    PreferenceManager.getDefaultSharedPreferences(context);
    // String profile = mSharedPreferences.getString("pref_profile_list", "-1");
    String profile = mSharedPreferences.getString("pref_login_list", "-1");
    Log.i(TAG, profile);
    return profile;
}
    void init() {

    String profile = getProfile(this);
    Log.i(TAG, "getProfile " + profile);

    switch (parseInt(profile)){
        case 0: 
            startActivity(new Intent(this, firstActivity.class));
            break;
        case 2: 
            startActivity(new Intent(this, secondActivity.class));
            break;
        default:
            Log.i(TAG, profile);

    }
}

Thanks.

Upvotes: 1

Views: 2633

Answers (4)

shweta_jain
shweta_jain

Reputation: 453

You can create an Class by extends Application class

PreAppResources.Java

public class PreAppResources extends Application{

    public static String execute="execute before mail activity"; 
    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();

        System.out.println(execute);
    }
}

AndroidManifest.xml

 <application
        android:allowBackup="true"
         android:name="com.example.listtt.PreAppResources"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
         android:largeHeap="true"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.listtt.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

If its not working please let me know i will try to help you more.

Upvotes: 0

Spring Breaker
Spring Breaker

Reputation: 8251

You can use the android:name="your class name" inside the <application> tag of the manifest file.

android:name : The fully qualified name of an Application subclass implemented for the application. When the application process is started, this class is instantiated before any of the application's components. The subclass is optional; most applications won't need one. In the absence of a subclass, Android uses an instance of the base Application class.

Example:

public class Platform extends Application {

public static String str="";
    @Override
    public void onCreate() {
        super.onCreate();
        str="I am executed first";
    }
}

To execute Platform before any other application's component add this Platform class to manifest file of your project like below,

<application
        android:name="com.example.Platform"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
        <activity
            android:name="com.example.HomeActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
  </application>

Hope it helps.

For reference read http://developer.android.com/guide/topics/manifest/application-element.html

Upvotes: 2

Swayam
Swayam

Reputation: 16364

The best option would be to use a Splash Screen.

The Splash Screen can be used for a variety of purposes including loading data, making calls to the server, showing the app logo and applying whatever logic you would want. You can think of it as an Activity which runs before your MainActivity and does the pre-processing, while showing a progress screen to the user.

Upvotes: 0

Rajesh Mikkilineni
Rajesh Mikkilineni

Reputation: 844

create a activity "UILApplication" for your resource initialization

and in the manifest include that

    **android:name="com.example.UILApplication"**

    android:label="@string/app_name"
    android:theme="@style/Theme.AppCompat.Light.DarkActionBar" >
    <activity
        android:name="com.example.HomePage"
        android:label="@string/app_name"
        >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
 ........

so that it will execute before your launch activity..

Hope this Helps you

Upvotes: 0

Related Questions