0

Problem Description

I have two activities in my application MainActivity and BannerActivity. From the main activity I start BannerActivity in onCreate method. But I first I see MainActivity screen for a second and then BannerActivity screen.

Question

How I can do so that BannerActivity will be shown first and after countdown timer will stop and BannerActivity will close after that MainActivity come to the screen.

MainActivity

public class MainActivity extends Activity {

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

    /* Show banner activity for several seconds then close it. */
    Intent bannerIntent = new Intent(MainActivity.this, BannerActivity.class);
    this.startActivity(bannerIntent);   
}
};

BannerActivity

public class BannerActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    /* Make banner fullscreen. */
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                         WindowManager.LayoutParams.FLAG_FULLSCREEN);

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_banner);

    /* Launch count down timer for several seconds. */
    CountDownTimer countDownTimer = new CountDownTimer(3000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) { /* Not used. */ }

        @Override
        public void onFinish() {
            BannerActivity.this.finish();
        }

    }.start();

}

@Override
public void onBackPressed() {
    /* Lock back button presses. */
    super.onBackPressed();
}

};
Viktor Apoyan
  • 10,655
  • 22
  • 85
  • 147
  • 1
    why don´t You start BannerActivity as your main activity? Then you could set here an intent after timer stops to start MainActivity... – Opiatefuchs May 27 '13 at 12:23
  • @ViTO you can just have setContentView replaced as required in MainActivity. – Arpit Garg May 27 '13 at 12:24
  • http://stackoverflow.com/questions/16750059/why-my-splash-screen-dont-show-the-images/16750316#16750316. Check this link. Might help you. – Raghunandan May 27 '13 at 12:35

8 Answers8

1

You need a SplashScreen:

http://www.thiagorosa.com.br/en/tutorial/part01-splash-screen

public class GameSplash extends Activity {

// time to wait on the splash screen
private static final int SPLASH_SCREEN_DELAY = 3000;

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

    new Thread() {
        @Override
        public void run() {
            try {
                // do any heavy initialization here

                // wait a few seconds before going to the next screen
                sleep(SPLASH_SCREEN_DELAY);
            }
            catch (InterruptedException e) {

            }
            catch (Exception e) {

            }
            finally {
                // start the level selection screen
                Intent intentSelect = new Intent(GameSplash.this, GameSelect.class);
                startActivity(intentSelect);
            }
        }
    }.start();

}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // ignore any key press on the splash screen
    return true;
}

}
thiagolr
  • 6,909
  • 6
  • 44
  • 64
0

try to use this way first need to load xml layout

requestWindowFeature(Window.FEATURE_NO_TITLE);

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_banner);
 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                         WindowManager.LayoutParams.FLAG_FULLSCREEN);

or try to put in manifest like

 <activity
        android:name=".BannerActivity"
         android:label="@string/app_name"
         android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
           >
Sunil Kumar
  • 7,086
  • 4
  • 32
  • 50
0

Run first BannerActivity and in the contdown run MainActivity rather than BannerActivity.this.finish();

mromer
  • 1,867
  • 1
  • 13
  • 18
0

You can start with BannerActivity and then when the time expires you go to MainActivity and clear the history stack by using this intent flag (so that you can't go back to the BannerActivity using the back button).

Intent.FLAG_ACTIVITY_CLEAR_TASK
Simon
  • 6,293
  • 2
  • 28
  • 34
0

You need splash screen. Try this:

Start BanerActivity first. And make its onCreate like

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

        Thread welcomeThread = new Thread() {

            int wait = 0;

            @Override
            public void run() {
                try {
                    super.run();

                    while (wait < 5000) { //Wait of 5 seconds
                        sleep(500);
                        wait += 500;
                    }
                } catch (Exception e) {

                } finally {

                    Intent i = new Intent(BanerActivity.this,
                            MainActivity.class);
                    startActivity(i);
                    finish();
                }
            }
        };
        welcomeThread.start();
    }

Hope this helps.

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
0

Swap the activites on the Manifest to decide which activity is run first :

    <activity
        android:name="com.yourpackagename.MainActivity"
        android:label="@string/app_name" >
    </activity>
    <activity
        android:name="com.yourpackagename.BannerActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

The BannerActivity should have the <intent-filter> tag and set as Main and Launcher as the example. This will make Banner activity as the Initial Activity when you start your application.

After that you can just implement countdowntimer and start MainActivity when the timer ends.

Hope this helps, Good Luck ^^

reidzeibel
  • 1,622
  • 1
  • 19
  • 24
0

you can do like this also in your Banner Activity

use this code in oncreate method in banner activity

Handler handler = new Handler();
    handler.postDelayed(new Runnable() {

        public void run() {

            finish();
        }
    }, 2000);
Senthil
  • 1,244
  • 10
  • 25
0

the easiest solution is to make the BannerActivity the launcher activity and modify the CountDownTimer to be

 @Override
 public void onFinish() 
 {
         Intent mainIntent = new Intent(this, MainActivity.class);
         startActivity(mainIntent); 
         finish();
 }

then adjust the manifest file to make the BannerActivity as the launcher activity and remove it from the MainActivity

Ahmed Daif
  • 379
  • 3
  • 12