这是我的代码。非常简单..但是当我点击后退按钮时,会出现第二个活动..
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Thread t = new Thread(){
public void run(){
try{
sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
Intent i = new Intent(MainActivity.this,SecondActivity.class);
startActivity(i);
}
}
};
t.start();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
this.finish();
}
}
我还放入了清单文件,但我仍然遇到同样的问题..
android:noHistory="true"
答案 0 :(得分:3)
} finally {
Intent intent = new Intent();
intent.setClass(SplashActivity.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivityForResult(intent, 0);
finish();
}
答案 1 :(得分:1)
我建议您切换到在doInBackground()期间休眠3秒的AsyncTask并启动新活动+在onPostExecute()中杀死当前的活动,但仅当isCancelled()不为真时。
然后覆盖你的splash活动的onBackPressed()并且:
这样AsyncTask仍然会在后台睡3秒,但是当它的onPostExecute()出现时,它会通过检查isCancelled()而不是启动第二个活动来知道它被取消了。
答案 2 :(得分:1)
使用Handler
这样制作Splash
屏幕
private static final int SPLASH_TIME_OUT = 3000;
private Handler mHandler = new Handler();
private Runnable mRunnable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
mRunnable = new Runnable() {
@Override
public void run() {
openLandingActivity();
}
};
mHandler.postDelayed(mRunnable, SPLASH_TIME_OUT);//
}
protected void openLandingActivity() {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent);
finish();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if(keyCode == KeyEvent.KEYCODE_BACK) {
mHandler.removeCallbacks(mRunnable);
finish();
}
return super.onKeyDown(keyCode, event);
}
答案 3 :(得分:0)
试试这个希望它会帮助你..
private final int SPLASH_DISPLAY_LENGHT = 5000;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.activity_main);
/* New Handler to start the Menu-Activity
* and close this Splash-Screen after some seconds.*/
new Handler().postDelayed(new Runnable(){
@Override
public void run() {
/* Create an Intent that will start the Menu-Activity. */
Intent i = new Intent(MainActivity.this,SecondActivity.class);
startActivity(i);
splash.this.finish();
}
}, SPLASH_DISPLAY_LENGHT);
}
}
答案 4 :(得分:0)
我不得不使用onKeyDown()方法使问题消失。这是固定代码..
public class MainActivity extends ActionBarActivity {
private boolean isBackPressed = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Thread t = new Thread(){
public void run(){
try{
sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(!isBackPressed){
Intent i = new Intent(MainActivity.this,SecondActivity.class);
startActivity(i);
}
}
}
};
t.start();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
isBackPressed = true;
finish();
}
return super.onKeyDown(keyCode, event);
}
}