【发布时间】:2016-02-08 21:37:15
【问题描述】:
我有一个主活动,我从中调用一个启动画面意图,它会在 3 秒后自行销毁,但在启动画面意图的生命周期之间,主活动也会自行销毁(这是错误的!).. 所以当启动画面Intent 完成了 App 崩溃了,因为 Main Activity 本身已经被销毁了。
如果有人可以帮助我,我真的很感激,我现在真的没有想法。
这是我的代码:
MainActivity.java
public class MainActivity extends Activity {
private WebView webview;
public MainActivity() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
log.debug("onCreate(): " + savedInstanceState);
MyApplication.startSomeMobileCore(this);
MyApplication.startSomeMobileNotifier(this);
setContentView(R.layout.main);
this.onNewIntent(this.getIntent());
}
@Override
protected void onStart() {
log.debug("onStart()");
super.onStart();
}
@Override
protected void onRestart() {
super.onRestart();
this.wasRestarted = true;
}
@Override
protected void onResume() {
super.onResume();
}
protected void onPause() {
super.onPause();
this.receivedIntent = false;
}
protected void onStop() {
super.onStop();
this.receivedIntent = false;
}
public void onDestroy() {
super.onDestroy();
}
@Override
public void onNewIntent(Intent intent) {
log.debug("onNewIntent(): " + intent);
super.onNewIntent(intent);
if(intent == null) {
log.warn("Received null intent, will ignore");
}
if ("OK".equals(authCode)) {
if (intent != null && intent.getData() != null &&
("content".equals(intent.getData().getScheme()) ||
"http".equals(intent.getData().getScheme()))) {
log.debug("intent.getData() :" + intent.getData() + "; intent.getData().getScheme() : " + intent.getData().getScheme());
String requestedPath;
if ("http".equals(intent.getData().getScheme())) {
requestedPath = URLDecoder.decode(intent.getData().toString());
} else {
requestedPath = intent.getData().getPath();
}
showResource(requestedPath);
} else {
log.debug("Intent without data -> go to entry page after splash screen");
showResource(Configuration.properties.getProperty("PORTAL"));
}
} else {
Intent errorIntent = new Intent(this, ErrorIntent.class);
startActivity(errorIntent);
// finish actual activity
finish();
}
log.debug("Show splash screen");
Intent intentSplash = new Intent(this, SplashIntent.class);
startActivity(intentSplash);
}
void showResource(String resourceToShow) {
webview = (WebView)findViewById(R.id.browser);
webview.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
webview.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
webview.setWebViewClient(new WebViewClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setDomStorageEnabled(true);
webview.loadUrl(resourceToShow);
}
}
}
这是我的 SplashIntent.java
public class SplashIntent extends Activity {
// Time splash screen should be shown (in ms)
private static final int splashTime = 3000;
static Logger log = Logger.getLogger(SplashIntent.class);
@Override
public void onCreate(final Bundle savedInstanceState) {
log.debug("SplashIntent: onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
log.debug("SplashIntent: killing splash");
finish();
}
}, splashTime);
}
}
【问题讨论】:
-
这没有多大意义。从
onCreate调用this.onNewIntent(this.getIntent());。authCode的默认值可能不是OK,因此您将启动ErrorIntent活动、finish()您的MainActivity,然后还启动SplashIntent活动。你希望它如何准确地工作?authCode什么时候设置? -
在所有这些验证之后,总是显示启动画面(3 秒)然后是主活动@GeorgeMulligan
标签: java android android-intent splash-screen