显示onCreate的活动

时间:2013-05-27 12:19:47

标签: java android android-activity

问题描述

我的应用MainActivityBannerActivity中有两项活动。在主要活动中,我在BannerActivity方法中启动onCreate。但我首先看到MainActivity屏幕一秒钟,然后是BannerActivity屏幕。

问题

如何执行此操作,BannerActivity将首先显示,倒数计时器将停止,BannerActivity将在MainActivity到达屏幕后关闭。

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

};

8 个答案:

答案 0 :(得分:1)

你需要一个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;
}

}

答案 1 :(得分:0)

尝试使用这种方式首先需要加载xml布局

requestWindowFeature(Window.FEATURE_NO_TITLE);

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

或尝试放入类似

的清单
 <activity
        android:name=".BannerActivity"
         android:label="@string/app_name"
         android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
           >

答案 2 :(得分:0)

首先运行BannerActivity并在收缩运行MainActivity而不是BannerActivity.this.finish();

答案 3 :(得分:0)

您可以从BannerActivity开始,然后在时间到期时转到MainActivity并使用此intent标志清除历史堆栈(这样您就无法使用后退按钮返回BannerActivity)。

Intent.FLAG_ACTIVITY_CLEAR_TASK

答案 4 :(得分:0)

你需要启动画面。试试这个:

首先开始BanerActivity。并使其onCreate喜欢

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

希望这有帮助。

答案 5 :(得分:0)

交换清单上的活动以决定首先运行哪个活动:

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

BannerActivity应该有<intent-filter>标记,并设置为Main和Launcher作为示例。这将使您在启动应用程序时将Banner活动作为初始活动。

之后,您可以实现countdowntimer并在计时器结束时启动MainActivity。

希望这有帮助,祝你好运^^

答案 6 :(得分:0)

你也可以在你的横幅活动

中这样做

在横幅活动中的oncreate方法中使用此代码

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

        public void run() {

            finish();
        }
    }, 2000);

答案 7 :(得分:0)

最简单的解决方案是使BannerActivity启动器活动并将CountDownTimer修改为

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

然后调整manifest文件,将BannerActivity作为launcher activity,并将其从MainActivity

中删除