0

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

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

因此,在 MagicPower 活动中,我想在 sleep() 时间后回到 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 Activity 不应再次出现在屏幕上!请帮助我提前谢谢

4

3 回答 3

2

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

于 2013-08-15T12:17:34.043 回答
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));
于 2013-08-15T12:22:45.157 回答
0

代替:

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

你可以使用这个:

intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

它将保留Activity.

于 2013-08-15T12:18:58.777 回答