Android 的“应用程序”启动屏幕班级
我有一个 Android 应用程序,我在其中扩展了基本 Application 类以设置一些全局变量。
public class MyApplication extends Application {
private ArrayList<ModelClass> master_list; // global variable 1
private DataBaseHelper db_helper; // global variable 2
@Override
public void onCreate() {
super.onCreate();
//do database work that will take about 5 seconds
}
}
我想在应用程序类工作时(即在创建我的主活动之前)向用户显示启动屏幕。有办法做到这一点吗?
I have an Android app where I've extended the base Application class in order to set up some global variables.
public class MyApplication extends Application {
private ArrayList<ModelClass> master_list; // global variable 1
private DataBaseHelper db_helper; // global variable 2
@Override
public void onCreate() {
super.onCreate();
//do database work that will take about 5 seconds
}
}
I'd like to show a splash screen to the user while the Application class is working (i.e. before my Main Activity is created). Is there a way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以将 SplashActivity 作为您的启动活动。
当 MyApplication 完成工作后,您可以开始主要活动并处理启动屏幕。
但是不要在 onCreate 中执行繁重的数据库工作,创建另一个函数并在那里执行它,否则您的启动活动将不会显示。
You could make the SplashActivity your start activity.
When MyApplication has completed it's work you could start your main activity and dispose the splash screen.
But don't do the heavy database work in onCreate, create another function and do it there, otherwise your splash activity won't be shown.
我在应用程序中执行此操作的方法是让您的主要活动扩展 ActivityGroup。
这样,当应用程序恢复时,您可以立即获取应用程序,而不是启动屏幕,并且只需加载一次数据(当应用程序启动时)。
我实际上用它来确保用户在应用程序启动时登录,如果用户名/密码组合不正确,我不会启动主应用程序,而是启动登录屏幕:)
The way I do this in my apps is by having your main activity extend ActivityGroup.
This way, when the app resumes you immediately get the app, not the splash screen, and you only load your data once (when the app starts).
I actually use this to make sure the user is logged in when the app starts, and if the username/password combination is not right I don't start the main app, but a login screen :)
首先,我认为有很多方法可以实现启动画面。直观上,我不会有一个特殊的类/活动只是为了加载特定信息,因为要保留该信息,您必须使该活动保持活动状态并在某处保持良好状态 - 占用资源。
我希望启动屏幕只是一个布局,最初使用主活动的 onCreate() 加载,然后加载数据库信息后,将布局更改为主活动布局。
像这样的东西:
First off, I think there are many approaches to having a splash screen. Intuitively, I would not have a special class/activity just to load specific information, because to keep that information, you will have to keep that activity alive and well somewhere- taking up resources.
I would have a splash screen just be a layout, that is initially loaded on with the main activity's onCreate(), and then once the DB information is loaded, change your layout to the main activities layout.
something like:
我意识到这已经是在问这个问题之后很久了,但我希望有人发现这很有用,如果需要的话我会提供代码。我的方法是在应用程序类中创建一个名为“finishedLoading”的公共布尔值。我将应用程序启动到启动屏幕 Activity 中,并且每 250 毫秒检查一次是否 finishLoading = true,如果是,我启动新的 Activity。另一个不错的部分是我有一个小旋转进度条,这样用户就可以看到正在发生的事情和周围的动画。
I realize this is rather far after this was asked but I hope someone finds this useful and I will provide code if needed. My approach was to create a public boolean in the application class called "finishedLoading". I launch the application into my splash screen activity and I have that check every 250 milliseconds as to whether or not finishedLoading = true and when it does, I launch the new activity. another nice part about that is I have a little spinner progress bar so the user sees somethings happening and animation all around.