Android应用程序仅在单击应用程序图标时才会从启动画面重新启动

时间:2014-03-15 10:43:21

标签: android

当我安装Android应用程序时,安装完成后有两个选项,“完成”和“打开”,如果我选择打开以运行已安装的应用程序,然后按主页按钮并单击应用程序图标(第一次单击应用程序icon)然后在应用程序运行时打开启动活动。问题是,如果应用程序已在后台运行,我不想调用启动活动。

活动流程: 1.闪屏延伸活动 2.主要活动扩展了SherlockFragmentActivity

公共类SplashScreen扩展了Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);
     /****** Create Thread that will sleep for 5 seconds *************/        
    Thread background = new Thread() {
        public void run() {

            try {
                // Thread will sleep for 5 seconds
                sleep(5*1000);

                // After 5 seconds redirect to another intent
                Intent i=new Intent(getBaseContext(),MainActivity.class);
                startActivity(i);

                //Remove activity
                finish();

            } catch (Exception e) {

            }
        }
    };

    // start thread
    background.start();

}
@Override
protected void onSaveInstanceState(Bundle outState) {
    // TODO Auto-generated method stub
    super.onSaveInstanceState(outState);
}

}

2 个答案:

答案 0 :(得分:2)

我有同样的问题,我设法通过下面的代码来解决它;

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

    if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
        // Activity was brought to front and not created,
        // Thus finishing this will get us to the last viewed activity
        finish();
        return;
    }

答案 1 :(得分:0)

您可以做的是:

private boolean mSplashShown;

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

    /****** Create Thread that will sleep for 5 seconds *************/
    Thread background = new Thread() {
        public void run() {
            try {
                // Thread will sleep for 5 seconds
                sleep(5 * 1000);

                // After 5 seconds redirect to another intent
                proceed();

                //Remove activity
                mSplashShown = true;
                finish();
            } catch (Exception e) {}
        }
    };

    // start thread
    if (mSplashShown) {
        proceed();
    } else {
        background.start();
    }
}

private void proceed() {
    final Intent i = new Intent(getBaseContext(), MainActivity.class);
    startActivity(i);
}

只要SplashActivity将保留在内存中,mSplashShown将保留适当的值并采取相应的行动。

另一种解决方案:

private static final String KEY_SHOW_SPLASH = "MyActivity.KEY_SHOW_SPLASH";

@Override
protected void onResume() {
    super.onResume();

    if (showSplash()) {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                setShowSplash(false);
                proceed();
                finish();
            }
        }, 2000);
    } else {
        proceed();
    }
}

private void proceed() {
    final Intent i = new Intent(this, MainActivity.class);
    startActivity(i);
}

@Override
protected void onDestroy() {
    super.onDestroy();
    setShowSplash(true);
}

private void setShowSplash(final boolean show) {
    final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    preferences.edit().putBoolean(KEY_SHOW_SPLASH, show).commit();
}

private boolean showSplash() {
    return PreferenceManager.getDefaultSharedPreferences(this).getBoolean(KEY_SHOW_SPLASH, true);
}