Android setContentView 的生命周期很短 Activity
Android setContentView on pretty short Activity life time
我的 android 应用程序的第一个 activity "launcher activity" 很快完成。它的目标是重定向到 MainActivity 或 UserLoginActivity,具体取决于共享首选项变量的值。
如果这个变量不存在,它会自动执行一个StartActivity到MainActivity。
如果设置了这个变量,那么它将对我的 API 执行 HTTP 请求,以便对用户进行身份验证。然后它将启动 MainActivity。 HTTP 请求通常不到一秒。
问题是我想在 LauncherActivity 的中心显示一个进度条,以便用户可以了解正在加载某些内容。
问题是屏幕上没有显示任何内容。但是,如果我注释掉 activity 开始的那一行,那么它将被显示...似乎 activity 持续时间太快而无法显示任何内容!
我认为调用 setContentView() 方法会立即在屏幕上加载视图。我的情况是正常行为吗?知道 activity 将持续大约一秒,我如何在屏幕上显示进度条?
在这里你可以看到我的启动器Activity
public class Launcher extends Activity {
private void goToUserLogin(){
Intent intent;
intent = new Intent(this, UserLoginActivity.class);
startActivity(intent);
finish();
}
private void goToMain(){
YokiAPI API = new YokiAPI(this);
Intent intent;
try {
if (API.authenticateSmart(YokiGlobals.preferences.getSavedUserId(this))) {
intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
} else {
this.goToUserLogin();
}
} catch (Exception e){}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launcher);
// Launch Demo if First Run
if (YokiGlobals.preferences.getFirstLaunch(this)){
YokiGlobals.preferences.updateFirstLaunch(false, this);
this.launchDemo();
}
/*
** If userId saved, smart Auth and go to Main
** Else go to User Login for Full Auth or register
*/
if (YokiGlobals.preferences.getSavedUserId(this) == null){
this.goToUserLogin();
}
else {
this.goToMain();
}
}
}
以及 .xml 资源文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="THIS TEXT WONT APPEAR"
android:layout_marginTop="208dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
谢谢,
奥斯卡
I thought calling the setContentView() method will instantly load Views on the screen
不,不会,因为您还在 onCreate()
。如果你想看到任何 UI 你需要让 activity 循环更进一步,所以重新编写你的代码或将你的身份验证移动到单独的 activity, meybe?
PS:您使用 this.
没有任何真正原因。
感谢您的帮助。我使用 AsyncTask 来从 API 中获取数据。
主 UI 线程现在可以加载视图。
这里有一些非常简化的代码,可能会对其他人有所帮助。
public class UserSmartAuthActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_smart_auth);
new SmartAuth().execute();
}
private class SmartAuth extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
Context context = getApplicationContext();
YokiAPI API = new YokiAPI(context);
Intent intent = null;
try {
if (API.authenticateSmart(YokiGlobals.preferences.getSavedUserId(context)))
intent = new Intent(context, MainActivity.class);
else
intent = new Intent(context, UserLoginActivity.class);
} catch (Exception e){}
startActivity(intent);
finish();
return null;
}
}
}
我的 android 应用程序的第一个 activity "launcher activity" 很快完成。它的目标是重定向到 MainActivity 或 UserLoginActivity,具体取决于共享首选项变量的值。
如果这个变量不存在,它会自动执行一个StartActivity到MainActivity。 如果设置了这个变量,那么它将对我的 API 执行 HTTP 请求,以便对用户进行身份验证。然后它将启动 MainActivity。 HTTP 请求通常不到一秒。
问题是我想在 LauncherActivity 的中心显示一个进度条,以便用户可以了解正在加载某些内容。 问题是屏幕上没有显示任何内容。但是,如果我注释掉 activity 开始的那一行,那么它将被显示...似乎 activity 持续时间太快而无法显示任何内容!
我认为调用 setContentView() 方法会立即在屏幕上加载视图。我的情况是正常行为吗?知道 activity 将持续大约一秒,我如何在屏幕上显示进度条?
在这里你可以看到我的启动器Activity
public class Launcher extends Activity {
private void goToUserLogin(){
Intent intent;
intent = new Intent(this, UserLoginActivity.class);
startActivity(intent);
finish();
}
private void goToMain(){
YokiAPI API = new YokiAPI(this);
Intent intent;
try {
if (API.authenticateSmart(YokiGlobals.preferences.getSavedUserId(this))) {
intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
} else {
this.goToUserLogin();
}
} catch (Exception e){}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launcher);
// Launch Demo if First Run
if (YokiGlobals.preferences.getFirstLaunch(this)){
YokiGlobals.preferences.updateFirstLaunch(false, this);
this.launchDemo();
}
/*
** If userId saved, smart Auth and go to Main
** Else go to User Login for Full Auth or register
*/
if (YokiGlobals.preferences.getSavedUserId(this) == null){
this.goToUserLogin();
}
else {
this.goToMain();
}
}
}
以及 .xml 资源文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="THIS TEXT WONT APPEAR"
android:layout_marginTop="208dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
谢谢, 奥斯卡
I thought calling the setContentView() method will instantly load Views on the screen
不,不会,因为您还在 onCreate()
。如果你想看到任何 UI 你需要让 activity 循环更进一步,所以重新编写你的代码或将你的身份验证移动到单独的 activity, meybe?
PS:您使用 this.
没有任何真正原因。
感谢您的帮助。我使用 AsyncTask 来从 API 中获取数据。 主 UI 线程现在可以加载视图。
这里有一些非常简化的代码,可能会对其他人有所帮助。
public class UserSmartAuthActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_smart_auth);
new SmartAuth().execute();
}
private class SmartAuth extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
Context context = getApplicationContext();
YokiAPI API = new YokiAPI(context);
Intent intent = null;
try {
if (API.authenticateSmart(YokiGlobals.preferences.getSavedUserId(context)))
intent = new Intent(context, MainActivity.class);
else
intent = new Intent(context, UserLoginActivity.class);
} catch (Exception e){}
startActivity(intent);
finish();
return null;
}
}
}