【发布时间】:2016-03-17 13:58:31
【问题描述】:
我只想在我的应用程序第一次启动时启动一个 Activity,从那时起,该应用程序每次都应该与另一个(启动器)Activity 一起启动。所以我实现了一个基于this SO answer的解决方案。
解决方案围绕boolean 首选项(以下代码中以startedBeforePreferenceKey 作为键的首选项)。在启动器活动的onCreate() 中,我尝试使用键startedBeforePreferenceKey 检索首选项并将其存储在变量startedBefore 中。如果首选项不存在,则将startedBefore 分配给false。
然后我检查startedBefore 是否为false,如果是,我创建提到的首选项,给它一个值true 并将其存储在SharedPreferences,然后启动这个应该第一次启动应用程序的活动。这样,当下次执行 onCreate() 时执行此检查时,startedBefore 将被分配 true,因此此启动一次活动将不会启动。
问题是,当应用程序首次启动时,正常的启动器会在 Activity 之前立即显示,只有在应用程序首次启动时才应该启动应用程序已启动。
第一次启动应用时,正常的启动器根本不应该显示。直接,我想在第一次启动应用程序时显示的特殊活动,应该显示。
我该怎么办?
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
boolean startedBefore = sharedPreferences.getBoolean(getString(R.string.startedBeforePreferenceKey), false);
if (!startedBefore) {
SharedPreferences.Editor sharedPreferencesEditor = sharedPreferences.edit();
sharedPreferencesEditor.putBoolean(getString(R.string.startedBeforePreferenceKey), true);
sharedPreferencesEditor.commit();
startActivity(new Intent(this, MainActivity.class));
}
编辑: @HammadTariqSahi
首先,这段摘自 LogCat:
03-16 08:42:25.629: E/AndroidRuntime(1837): FATAL EXCEPTION: main
03-16 08:42:25.629: E/AndroidRuntime(1837): Process: tests.globalactivitytest, PID: 1837
03-16 08:42:25.629: E/AndroidRuntime(1837): java.lang.RuntimeException: Unable to instantiate application tests.globalactivitytest.activity.GlobalActivity: java.lang.ClassNotFoundException: Didn't find class "tests.globalactivitytest.activity.GlobalActivity" on path: DexPathList[[zip file "/data/app/tests.globalactivitytest-1/base.apk"],nativeLibraryDirectories=[/data/app/tests.globalactivitytest-1/lib/x86, /vendor/lib, /system/lib]]
03-16 08:42:25.629: E/AndroidRuntime(1837): at android.app.LoadedApk.makeApplication(LoadedApk.java:578)
03-16 08:42:25.629: E/AndroidRuntime(1837): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4680)
03-16 08:42:25.629: E/AndroidRuntime(1837): at android.app.ActivityThread.-wrap1(ActivityThread.java)
03-16 08:42:25.629: E/AndroidRuntime(1837): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1405)
03-16 08:42:25.629: E/AndroidRuntime(1837): at android.os.Handler.dispatchMessage(Handler.java:102)
03-16 08:42:25.629: E/AndroidRuntime(1837): at android.os.Looper.loop(Looper.java:148)
03-16 08:42:25.629: E/AndroidRuntime(1837): at android.app.ActivityThread.main(ActivityThread.java:5417)
03-16 08:42:25.629: E/AndroidRuntime(1837): at java.lang.reflect.Method.invoke(Native Method)
03-16 08:42:25.629: E/AndroidRuntime(1837): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
03-16 08:42:25.629: E/AndroidRuntime(1837): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
03-16 08:42:25.629: E/AndroidRuntime(1837): Caused by: java.lang.ClassNotFoundException: Didn't find class "tests.globalactivitytest.activity.GlobalActivity" on path: DexPathList[[zip file "/data/app/tests.globalactivitytest-1/base.apk"],nativeLibraryDirectories=[/data/app/tests.globalactivitytest-1/lib/x86, /vendor/lib, /system/lib]]
03-16 08:42:25.629: E/AndroidRuntime(1837): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
03-16 08:42:25.629: E/AndroidRuntime(1837): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
03-16 08:42:25.629: E/AndroidRuntime(1837): at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
03-16 08:42:25.629: E/AndroidRuntime(1837): at android.app.Instrumentation.newApplication(Instrumentation.java:981)
03-16 08:42:25.629: E/AndroidRuntime(1837): at android.app.LoadedApk.makeApplication(LoadedApk.java:573)
03-16 08:42:25.629: E/AndroidRuntime(1837): ... 9 more
03-16 08:42:25.629: E/AndroidRuntime(1837): Suppressed: java.lang.ClassNotFoundException: tests.globalactivitytest.activity.GlobalActivity
03-16 08:42:25.629: E/AndroidRuntime(1837): at java.lang.Class.classForName(Native Method)
03-16 08:42:25.629: E/AndroidRuntime(1837): at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
03-16 08:42:25.629: E/AndroidRuntime(1837): at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
03-16 08:42:25.629: E/AndroidRuntime(1837): at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
03-16 08:42:25.629: E/AndroidRuntime(1837): ... 12 more
03-16 08:42:25.629: E/AndroidRuntime(1837): Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available
GlobalActivity.java:
package tests.globalactivitytest;
import android.app.Application;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
public class GlobalActivity extends Application {
@Override
public void onCreate() {
super.onCreate();
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
//SharedPreferences.Editor editor = sharedPreferences.edit();
boolean launchedBefore = sharedPreferences.getBoolean("launchedBefore", false);
if (launchedBefore) {
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
} else {
Intent intent = new Intent(this, LaunchOnceActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
}
MainActivity.java:
package tests.globalactivitytest;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
LaunchOnceActivity.java:
package tests.globalactivitytest;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class LaunchOnceActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launch_once);
}
}
清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="tests.globalactivitytest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="23" />
<application
android:name=".activity.GlobalActivity"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".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>
<activity
android:name=".LaunchOnceActivity"
android:label="@string/title_activity_launch_once" >
</activity>
</application>
</manifest>
【问题讨论】:
-
如果没有某种“闪存”,您将无法真正处理这个问题。我的建议是,将启动屏幕视图作为您启动的活动并从那里处理重定向,这样无论加载哪个活动,您都可以获得持续的体验。
-
您应该使用某种 SplashScreen(如@zgc7009 所说)以及使用应用程序版本等(共享首选项将是存储数据的好地方)。官方 Android 不提供您打算做的事情,这是缺点。
-
@zgc7009 你看过 Instagram 吗? Instagram、Quora、WhatsApp,它们都要求您在首次启动应用程序时注册或登录。他们使用这样的黑客吗?我不认为他们会使用 hacky 解决方案,因为他们是科技巨头并且拥有最好的工程师。 :s
-
是的,当然。这根本不是 hack,它实际上是 Android 开发的一个非常常见的部分,我的很多应用程序都有启动画面。它们甚至记录在官方文档中。史密斯先生在下面有一个可行的答案。
-
尝试使用可见性。
标签: android android-activity oncreate onresume android-launcher