Rafael Reyes February 2016

Main Activity Destroyed before Splash Screen return

I have a Main Activity from where I call an Splash Screen Intent which destroys itself after 3 seconds but between the lifecycle of the Splash Screen Intent the Main Activity destroys itself too (which is wrong!).. so when the Splash Screen Intent is finished the App crashes because the Main Activity has been destroyed itself.

I really Appreciate if someone can help me with this, I'm really out of ideas at this point.

Here's my code:

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.ge        

Answers


Helix February 2016

There doesn't seem to be any reason to override onNewInent in your MainActivity.

In the onCreate() method use the following:

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

This will start the splash screen whenever the MainActivity is initialized without a saved state. Since your SplashIntent activity calls finish after it is done it should revert to the last activity in the stack (aka your MainActivity).


An even better way to do this would be to use your SplashIntent activity as your launcher activity and then forward the user to the MainActivity using an intent.

Very simple example would be:

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


Guru_VL February 2016

Try startActivityForResult to launch splash screen (SplashIntent).

instead of

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

Try the below

startActivityForResult

And then from SplashIntent.java

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

Ref link : http://developer.android.com/training/basics/intents/result.html

Sample code :

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.
           }
       }
   }
}

Splash Screen :

public class SplashActivity extends 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( 

Post Status

Asked in February 2016
Viewed 2,237 times
Voted 5
Answered 2 times

Search




Leave an answer