Joh*_*man 0 android android-manifest android-activity
在我的AndroidManifest.xml文件中,我有两个活动:
<activity android:name=".activities.LoginActivity"/>
<activity android:name=".activities.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Run Code Online (Sandbox Code Playgroud)
根据我的应用逻辑,如果我已登录,则会直接重定向到MainActivity
,否则重定向到LoginActivity
。第一次打开应用程序时,它LoginActivity
是打开的,但在后台MainActivity
也被称为。如何阻止这种情况的发生?但是没有以LoginActivity
做为主要活动吗?
我一直采用的解决方案是创建第三个活动 SplashScreenActivity
<activity
android:name=".activities.SplashScreenActivity"
android:theme="@style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Run Code Online (Sandbox Code Playgroud)
这不过是一个加载活动,您可以在其中实例化整个应用程序所需的所有内容,并在其中进行此逻辑。
你可以,例如,在调用这个SplashActivity
的OnCreate
:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(/*logics to see if user is logged*/) {
startActivity(SplashScreenActivity.this, MainActivity.class);
}
else {
startActivity(SplashScreenActivity.this, LoginActivity.class);
}
finish(); //finish the splash activity.
}
Run Code Online (Sandbox Code Playgroud)
另一个小把戏:)
注释中来自PPartisan的链接:如何实现SplashScreen
我总是尽量减少这种“ activity
布局”问题,以使您在首次启动应用程序时没有那种烦人的“黑屏闪烁”。为了做到这一点,如您所见,我theme
在该活动的清单中指定了一个simply
设置位置:
<style name="SplashTheme" parent="AppTheme">
<item name="android:windowBackground">@drawable/background_splash</item>
</style>
Run Code Online (Sandbox Code Playgroud)
该资源不过是drawable
带有的资源background
(例如,白色背景和徽标位于中心)。
这样,您在运行应用程序时就不会出现黑色闪烁。
祝好运!
归档时间: |
|
查看次数: |
65 次 |
最近记录: |