MainActivity 在 Splash Screen LifeCycle 之间自行销毁
Posted
技术标签:
【中文标题】MainActivity 在 Splash Screen LifeCycle 之间自行销毁【英文标题】:MainActivity destroys itself between Splash Screen LifeCycle 【发布时间】:2016-02-09 12:58:10 【问题描述】:我有一个 SplashScreen 和一个 MainActivity,当应用程序启动时它会显示启动画面(3 秒延迟)然后是 MainActivity,但是当我单击 mi 应用程序的 BarNotification(应用程序外部)时,启动画面显示(3秒延迟)和应用程序崩溃,在 LogCat 中,他 MainActivity 在 Splash Screen Intent LifeCycle 之间自行销毁。 (在第 29,30 行 logcat)
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_
android:layout_>
<WebView
android:id="@+id/browser"
android:layout_
android:layout_
/>
</LinearLayout>
splash.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_
android:layout_
android:gravity="center"
android:background="@color/white"
>
<ImageView android:layout_
android:contentDescription="splash screen"
android:id="@+id/splash"
android:src="@drawable/splash"
android:layout_
android:scaleType="centerInside"/>
</LinearLayout>
为什么我无法通过栏通知正确启动应用程序?
这是一些代码:
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);
onNewIntent(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(""));
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);
logcat
希望你们能帮我解决这个问题......我现在真的没有想法
【问题讨论】:
堆栈跟踪也会有所帮助。编辑:对不起,我刚刚意识到你添加了链接。 不要在run()中完成splashActivity,而是在run()方法中使用intent移动到MainActivity() 您的主要活动是否已声明为全球活动? 当我从 SplashIntent 移动到 Main Activity 时,我得到一个循环,其中 SplashINtent 执行 n 次并且永远不会到达 MainActivity @kishorepatel 试试这个代码:code
mSplashThread = new Thread() @Override public void run() try synchronized (this) wait(2000); catch (InterruptedException ex) finish();意图意图 = new Intent(); intent.setClass(sPlashScreen, SlideMainActivity.class);开始活动(意图); ; mSplashThread.start(); code
【参考方案1】:
在你的 SplashIntent
类中试试这个
Handler handler = new Handler();
handler.postDelayed(new Runnable()
@Override
public void run()
Intent mainIntent = new Intent(Splash.this,MainActivity.class);
SplashIntent.this.startActivity(mainIntent);
SplashIntent.this.finish();
,splashTime);
【讨论】:
感谢您的回答,我在 Splash 中有一个循环,所以永远不要到达 MainActivity。以上是关于MainActivity 在 Splash Screen LifeCycle 之间自行销毁的主要内容,如果未能解决你的问题,请参考以下文章
Android 使用 Splash Screen 加载繁重的 Main 数据