Android 启动屏幕期间加载Main活动
我目前有一个Android 启动屏幕期间加载Main活动,android,splash-screen,Android,Splash Screen,我目前有一个splashScreenActivity,要求用户按按钮转到main活动 是否可以加载MainActivity的所有内容,而MainActivity的UI不会出现在splashScreenActivity的UI上方,这样当他按下按钮时,他会重定向到MainActivity,并且所有数据都100%加载 提前感谢1)当用户在SplashScreenActivity中时,使用AsyncTask在后台加载所有MainActivity内容。这将帮助您避免点击按钮将您从SplashScreenA
splashScreenActivity
,要求用户按按钮
转到main活动
是否可以加载MainActivity
的所有内容,而MainActivity
的UI不会出现在splashScreenActivity
的UI上方,这样当他按下按钮时,他会重定向到MainActivity
,并且所有数据都100%加载
提前感谢1)当用户在SplashScreenActivity中时,使用AsyncTask在后台加载所有MainActivity内容。这将帮助您避免点击按钮将您从SplashScreenActivity带到MainActivity的额外步骤,这将通过使用意图来处理。(请参阅下面的工作示例)
1) Use AsyncTask<> to load all your MainActivity contents in the background while the user is in the SplashScreenActivity. This will help you avoid the extra step of clicking on a button to take you from the SplashScreenActivity to the MainActivity, and this will be handled through the use of Intents. (Refer to working example below)
------------------------------------------------------------------
SplashScreenActivity.java
------------------------------------------------------------------
package foo.foo.load.mainactivity
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.AssetManager;
import android.content.res.Configuration;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
public class SplashScreenActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
// Make call to execute AsycTasks<> here
// This helps avoid the extra step of clicking on a button
// to take you to the MainActivity
new StartMainActivity().execute(this);
Thread timerThread = new Thread() {
public void run() {
try {
sleep(2000);
} catch(InterruptedException e) {
e.printStackTrace();
} finally {
// After 2 seconds the Splashscreen will disappear and user is taken to MainActivity
Intent splashScreenIntent = new Intent(SplashScreenActivity.this, MainActivity.class);
startActivity(splashScreenIntent);
}
}
};
timerThread.start();
}
@Override
protected void onPause() {
super.onPause();
finish();
}
private class StartMainActivity extends AsyncTask<Context, Void, Intent> {
Context ctx;
@Override
protected Intent doInBackground(Context... params) {
ctx = params[0];
AssetManager assetManager = ctx.getAssets();
final CBLite cblite = new CBLite(new AndroidContext(ctx), assetManager);
// Handle all your MainActivity Contents call here
// Begin MainActivity Content Calls
Image.getImages();
Navigation.getMainNavigation();
// End MainActivity Content Calls
Intent in = new Intent();
in.setClass(ctx, MainActivity.class);
return in;
}
@Override
protected void onPostExecute(Intent intent) {
ctx.startActivity(intent);
super.onPostExecute(intent);
}
}
}
----------------------------------------------------------------
activity_splash.xml
----------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="@drawable/splashscreen_background_image"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/splashScreenMainTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="150dp"
android:gravity="center"
android:text="Header Title"
android:visibility="visible"/>
<TextView
android:id="@+id/splashScreenSubTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/splashScreenMainTitle"
android:layout_marginBottom="40dp"
android:gravity="center"
android:text="Sub Header Title"
android:visibility="visible"/>
<ImageView
android:id="@+id/splashScreenLogo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/splashScreenSubTitle"
android:src="@drawable/logo"
android:layout_gravity="center_horizontal"
android:background="@android:color/transparent"/>
</RelativeLayout>
-----------------------------------------------------------------
activity_main.xml
-----------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_menu_search" />
</android.support.design.widget.CoordinatorLayout>
------------------------------------------------------------------
SplashScreenActivity.java
------------------------------------------------------------------
包foo.foo.load.main活动
导入android.app.Activity;
导入android.content.Context;
导入android.content.Intent;
导入android.content.pm.ActivityInfo;
导入android.content.res.AssetManager;
导入android.content.res.Configuration;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.util.Log;
导入java.util.ArrayList;
导入java.util.List;
公共类活动扩展了活动{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
//在此处调用以执行asyctask
//这有助于避免点击按钮的额外步骤
//带你去主要活动
新建StartMainActivity().execute(此);
线程时间读取=新线程(){
公开募捐{
试一试{
睡眠(2000年);
}捕捉(中断异常e){
e、 printStackTrace();
}最后{
//2秒后,Splashscreen将消失,用户进入MainActivity
意向splashScreenIntent=新意向(SplashScreenActivity.this,MainActivity.class);
星触觉(目的);
}
}
};
timerThread.start();
}
@凌驾
受保护的void onPause(){
super.onPause();
完成();
}
私有类StartMainActivity扩展了异步任务{
上下文ctx;
@凌驾
受保护的意图doInBackground(上下文…参数){
ctx=参数[0];
AssetManager AssetManager=ctx.getAssets();
最终CBLite CBLite=新的CBLite(新的AndroidContext(ctx),assetManager);
//在此处处理所有主要活动内容呼叫
//开始MainActivity内容调用
Image.getImages();
getMainNavigation();
//结束活动内容调用
Intent in=新Intent();
in.setClass(ctx,MainActivity.class);
返回;
}
@凌驾
受保护的void onPostExecute(意图){
星触觉(意图);
super.onPostExecute(intent);
}
}
}
----------------------------------------------------------------
activity_splash.xml
----------------------------------------------------------------
-----------------------------------------------------------------
activity_main.xml
-----------------------------------------------------------------
我找到了问题的答案
注意,在我的情况下,main活动可以是任何活动
将启动屏幕作为片段
而不是活动
允许您将main活动
与片段
重叠,同时main活动
数据在后台加载
此时,只要准备好,只需将
片段的可见性设置为View.GONE
或将其从片段堆栈中弹出getFragmentManager().popBackStack()代码>,您将返回(从未真正离开)到您的Main活动
,并加载所有数据。在主活动上使用带Runnable的全屏对话框
public void showsplash() {
final Dialog dialog = new Dialog(MainActivity.this, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.activity_splash_screen);
dialog.setCancelable(true);
dialog.show();
final Handler handler = new Handler();
final Runnable runnable = new Runnable() {
@Override
public void run() {
{
dialog.dismiss();
}
}
};
handler.postDelayed(runnable, 30000);
}
使用闪屏作为其他元素的布局。然后单击“不可见”。如果您正在使用该服务,您可以在后台启动它,同时在启动屏幕上启动该服务。@Elisfazel然后每次创建活动时他都会看到启动?即使主活动将其布局设置为自己的xml?我尝试了asynctask,但是mainActivity的布局文件只覆盖了我的splashscreen布局,因为它异步加载@Ibrahimali请尝试为splash创建一个活动,为mainActivity创建另一个活动,这样您就可以使用IntentService
在splash中加载数据。我已经尝试过了。但是如果我的SplashActivity顶部没有显示UI,我就无法加载MainActivity的内容,也许我做错了什么?为每个活动创建两个单独的布局文件,即:(1)MainActivity.java与Activity_main.xml文件绑定(2)SplashScreenActivity.java与Activity_splash.xml绑定。这将确保