gpt4 book ai didi

java - 终止具有主线程的 Android Activity

转载 作者:行者123 更新时间:2023-12-01 23:38:55 26 4
gpt4 key购买 nike

我的 Activity 流程有问题。流程如下:

 Splash -> MainMenu -> Launching -> Starting -> Instructions -> MagicPower

因此,在 MagicPower Activity 中,我想在 sleep() 时间之后返回到 MainMenu! 问题是 MainMenu Activity 根本没有完成,并且显示了两次。我该如何克服这个问题!正如我一直在努力的那样:

  package com.app.imagic;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class MagicPower extends Activity {

///////////
private TextView lookme;
private volatile boolean running=true;
private int i;
private TextView gone;

///////////
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_magic_power);
lookme=(TextView)findViewById(R.id.LOOK);
lookme.setVisibility(View.INVISIBLE);
gone=(TextView)findViewById(R.id.GONE);
gone.setVisibility(View.INVISIBLE);


Typeface face=Typeface.createFromAsset(getAssets(), "fonts/crazy.TTF");
gone.setTypeface(face);
lookme.setTypeface(face);


//////////////////////////////////////////////////////////
runThread();
}
//////////////////////////////////////////////////////////////
private void runThread() {
new Thread() {
public void run() {
while(running){
while ( i++ < 4) {

if(i==3){

Intent intent = new Intent(getApplicationContext(), MainMenu.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

startActivity(intent);
startActivity(new Intent(MagicPower.this,MainMenu.class));

running=false;
finish();
break;
}
try {
runOnUiThread(new Runnable() {
@Override
public void run() {
if(i==1){
lookme.setVisibility(View.VISIBLE);
}

if(i==2){
gone.setVisibility(View.VISIBLE);
}


}
});
Thread.sleep(2500);
} catch (InterruptedException e) {
e.printStackTrace();
return;
}
}
}
}
}.start();
}
///////////////////////////////////////////////////////////////////
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.magic_power, menu);
return true;
}
@Override
public void onBackPressed() {
// do nothing.
}

}

它应该像MainMenu之后一样; MainMenu Activity 不应再次出现在屏幕上!请帮助我提前致谢

最佳答案

尝试改变android:launchMode AndroidManifet.xml 中的 MainMenu Activity 。也许,“singleTop”就是您所需要的。

关于java - 终止具有主线程的 Android Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18252367/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com