杀死具有主线程的Android Activity

时间:2013-08-15 12:11:43

标签: java android android-activity

我的活动流程有问题。流程如下:

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

所以在MagicPower活动中,我想在睡眠()时间之后回到MainMenu!      问题是MainMenu活动根本没有完成,并且显示自己两次。我怎样才能克服这个问题!正如我一直在尝试的那样:

  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活动不应再次出现在屏幕上!请提前帮助我

3 个答案:

答案 0 :(得分:2)

尝试在AndroidManifet.xml中更改MainMenu活动的android:launchMode。可能,“ singleTop ”可能就是您所需要的。

答案 1 :(得分:1)

请在此处查看此代码:

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

您自己开始MainMenu次活动两次。来到这里:

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

startActivity(intent);

然后再说:

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

我建议你删除这一行:

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

答案 2 :(得分:0)

而不是:

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

您可以使用:

intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

它将保留Activity的状态。