android - 主要 Activity 执行前 "initialization"

标签 android

这个问题可以通过两种方式提出: - 我如何在启动主要 Activity 之前对变量和数据进行一些处理? - 我如何根据某些逻辑设置主启动器 Activity ?即在查看一组 Activity 中的 Activity 之前,我应该从首选项中检索数据。并且这应该只在第一次使用时完成,没有将首选项 Activity 保存在后退按钮堆栈中。

    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);

    }
}

谢谢。

最佳答案

您可以使用 android:name="your class name"<application>里面 list 文件的标签。

机器人:名字 : 为应用程序实现的应用程序子类的完全限定名称。当应用程序进程启动时,此类在应用程序的任何组件之前被实例化。 子类是可选的;大多数应用程序不需要一个。在没有子类的情况下,Android 使用基 Application 类的实例。

例子:

public class Platform extends Application {

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

执行Platform在任何其他应用程序的组件之前添加此 Platform类来 list 你的项目文件,如下所示,

<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>

希望对您有所帮助。

引用阅读http://developer.android.com/guide/topics/manifest/application-element.html

关于android - 主要 Activity 执行前 "initialization",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23243286/

相关文章:

android - 添加库项目后找不到类异常

android - 在 Headless 模式下运行 Genymotion 2.8

android - 如何将 TextView 的矩形修改/更改为其他内容

每次重新启动 Android Studio 时,Android Activity 主题都会更改

android - Firebase - 每个用户的唯一 ID

android - 如何使用 Google Assist API 实现智能助理

android - 在应用程序内截取屏幕截图

java - 如何使用 MusicPlayService 播放音乐

java - 无法转换为 Java 中的扩展类

android - 在 samsung s2 上测试应用程序时获取 Resources$NotFoundException