在Splash Screen返回之前销毁主要活动

时间:2016-02-08 21:37:16

标签: java android android-intent splash-screen

我有一个主要活动,我称之为Splash Screen Intent,它会在3秒后摧毁自己但在Splash Screen Intent的生命周期之间主要活动也会自行销毁(这是错误的!)所以当启动画面时意图完成了应用程序崩溃,因为主要活动已被破坏。

我真的很感激,如果有人可以帮助我,我现在真的没有想法。

这是我的代码:

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);

    }
}

这是一个 part of logcat

2 个答案:

答案 0 :(得分:1)

似乎没有任何理由在MainActivity中覆盖onNewInent。

在onCreate()方法中使用以下内容:

if(savedInstanceState == null){
    Intent splashIntent = new Intent(this, SplashIntent.class);
    startActivity(splashIntent);
}

只要在没有保存状态的情况下初始化MainActivity,这将启动启动屏幕。由于您的SplashIntent活动在完成后调用完成,它应该恢复到堆栈中的最后一个活动(也就是您的MainActivity)。

更好的方法是使用SplashIntent活动作为启动器活动,然后使用意图将用户转发到MainActivity。

非常简单的例子是:

public class SplashIntent extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(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");
             Intent intent = new Intent(this, MainActivity.class);
             startActivity(intent);
             finish();
         }
    }, splashTime);
  }
}

答案 1 :(得分:1)

尝试使用startActivityForResult启动启动画面(SplashIntent)。

而不是

Intent intentSplash = new Intent(this, SplashIntent.class); 
startActivity(intentSplash);

尝试以下

startActivityForResult

然后从SplashIntent.java

Intent i = new Intent();
setResult(Activity.RESULT_OK,i); //pass your result
finish(); // Call finish to remove splash from the stack

参考链接: http://developer.android.com/training/basics/intents/result.html

示例代码:

public class MainActivity extends Activity {
    static final int SHOW_SPLASH_SCREEN_REQUEST = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        showSplashSCreen();
   }

   private void showSplashSCreen() {
       Intent intentSplash = new Intent(this, SplashActivity.class);
       startActivityForResult(intentSplash, 
                                   SHOW_SPLASH_SCREEN_REQUEST);
   }

   @Override
   protected void onActivityResult(int requestCode, int resultCode,  
                                     Intent data) {
       // Check which request we're responding to
       if (requestCode == SHOW_SPLASH_SCREEN_REQUEST) {
           // Make sure the request was successful
           if (resultCode == RESULT_OK) {
              // code to handle anything after splash screen finished.
           }
       }
   }
}

启动画面:

公共类SplashActivity扩展了Activity {

private static final int splashTime = 3000;

@Override
public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);

    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        public void run() {
            // optional per your requirement
            setResult(MainActivity.SHOW_SPLASH_SCREEN_REQUEST);
            // must call finish
            finish();
        }
    }, splashTime);
}
}