Activity.finish()完成所有活动

时间:2016-07-01 08:55:07

标签: android android-activity

我有两个活动:MenuScreen和Main。 MenuScreen启动Main并完成自己。

Intent intent = new Intent(this, sp.overview.Main.class);
//All extra intent values
startActivity(intent);
finish();

然而,当我完成Main并启动MenuScreen时,它也会完成MenuScreen。请注意,在MainView的SurfaceView中调用光洁度

Intent i = new Intent(context, MenuScreen.class);
((Main)context).finish();
context.startActivity(i);

但我只想完成Main并返回MenuScreen。我做错了什么?

清单:

<application
    android:name=".Global"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme"
    android:largeHeap="true">
    <activity
        android:name=".MenuScreen"
        android:label="@string/app_name"
        android:screenOrientation="landscape" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".Main"
        android:label="@string/title_activity_menu_screen"
        android:screenOrientation="landscape">
    </activity>
</application>

MenuScreen:

public class MenuScreen extends Activity {

int subject_value = 0;
boolean training = false;
Global global;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    global = (Global)getApplicationContext();
    this.subject_value = global.getSubject();

    //Set to full screen
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_menu_screen);
    TextView t = (TextView)findViewById(R.id.subject_value);
    t.setText(String.valueOf(this.subject_value));

}

@Override
protected void onRestart()
{
    super.onRestart();
    this.subject_value = global.getSubject();
    TextView t = (TextView)findViewById(R.id.subject_value);
    t.setText(String.valueOf(this.subject_value));
}

@Override
protected void onStart()
{
    super.onStart();
    this.subject_value = global.getSubject();
    TextView t = (TextView)findViewById(R.id.subject_value);
    t.setText(String.valueOf(this.subject_value));
}

@Override
protected void onResume()
{
    super.onResume();
    this.subject_value = global.getSubject();
    TextView t = (TextView)findViewById(R.id.subject_value);
    t.setText(String.valueOf(this.subject_value));
}

public void StillOne(View view){
    sentExtra(Setting.Layout.Still, Setting.FrameSkip.One);
}

public void sentExtra(Setting.Layout layout, Setting.FrameSkip frameSkip)
{
    Intent intent = new Intent(this, sp.overview.Main.class);
    intent.putExtra("LAYOUT", layout.getValue());
    intent.putExtra("FRAMESKIP", frameSkip.getValue());
    intent.putExtra("SUBJECT", this.subject_value);
    intent.putExtra("TRAINING", this.training);
    finish();
    startActivity(intent);

}

public void MotionThree(View view) {
    sentExtra(Setting.Layout.Motion, Setting.FrameSkip.Three);
}

public void MotionTwo(View view) {
    sentExtra(Setting.Layout.Motion, Setting.FrameSkip.Two);
}

public void MotionOne(View view) {
    sentExtra(Setting.Layout.Motion, Setting.FrameSkip.One);
}

public void StillThree(View view) {
    sentExtra(Setting.Layout.Still, Setting.FrameSkip.Three);
}

public void StillTwo(View view) {
    sentExtra(Setting.Layout.Still, Setting.FrameSkip.Two);
}

public void Plus(View view) {
    this.subject_value++;
    global.setSubject(this.subject_value);
    TextView t = (TextView)findViewById(R.id.subject_value);
    t.setText(String.valueOf(this.subject_value));
}

public void Minus(View view) {
    this.subject_value--;
    global.setSubject(this.subject_value);
    TextView t = (TextView)findViewById(R.id.subject_value);
    t.setText(String.valueOf(this.subject_value));
}

public void Training(View view) {
    this.training = !this.training;
    android.widget.Button b = (android.widget.Button)findViewById(R.id.training);
    b.setText("Training: " + String.valueOf(this.training));

}
}

主:

public class Main extends Activity implements SensorEventListener {

private Overview overview;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //turn the title off
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    //set to full screen
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_FULLSCREEN
            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);

    Intent it = getIntent();
    int layout = it.getIntExtra("LAYOUT", 1);
    int frameskip = it.getIntExtra("FRAMESKIP", 1);
    int subject = it.getIntExtra("SUBJECT", 0);
    boolean training = it.getBooleanExtra("TRAINING", false);
    Setting s = new Setting(Setting.Layout.getSetting(layout), Setting.FrameSkip.getSetting(frameskip), training);
    overview = new Overview(this, s, subject);


    setContentView(overview);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

}



@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    return false;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    return false;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
public void onSensorChanged(SensorEvent event) {

}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {

}

}

概述:

public class Overview extends SurfaceView implements SurfaceHolder.Callback{

private MainLoop loop;
private static Context context;
public int Subject;
public Setting state;

public Overview(Context context, Setting state, int subject) {
    super(context);

    // Set the context;
    this.context = context;
    this.state = state;
    this.Subject = subject;

    //add the callback to the SurfaceHolder to intercept events;
    getHolder().addCallback(this);
    getHolder().setFormat(PixelFormat.RGBA_8888);
    //init the MainLoop
    loop = new MainLoop(getHolder(), this, this.state);
    setFocusable(true);
}

@Override
public boolean onTouchEvent(MotionEvent event)
{
    if(!loop.equals(null))
    {
        int index = event.getActionIndex();
        int id = event.getPointerId(index);
        ProgressManager p = loop.progressManager;
        PointF pos = null;
        switch (event.getActionMasked())
        {
            case MotionEvent.ACTION_UP:
                switch (p.state) {
                    case Start:
                        if (!p.loading)
                            p.state = ProgressManager.Progress.Image;
                        break;
                    case Image:
                        break;
                    case Pause:
                        p.state = ProgressManager.Progress.Overview;
                        break;
                    case Overview:
                        pos = new PointF(event.getX(0), event.getY(0));
                        if(p.trueButton.collide(pos)) {
                            p.answer = true;
                            if(p.setting.layout == Setting.Layout.Motion)
                                p.frameLoader.stop();
                            p.state = ProgressManager.Progress.Result;
                        }
                        if(p.falseButton.collide(pos)) {
                            p.answer = false;
                            if(p.setting.layout == Setting.Layout.Motion)
                                p.frameLoader.stop();
                            p.state = ProgressManager.Progress.Result;
                        }
                        break;
                    case Result:
                        if(!p.saving && p.saved) {
                            Intent i = new Intent(context, MenuScreen.class);
                            context.startActivity(i);
                            ((Main)context).finish();
                        }
                        break;
                }
                break;
            default:break;
        }
    }
    return true;
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    boolean retry = true;
    while(retry)
    {
        try
        {
            loop.setRunning(false);
            loop.join();
            retry = false;
            // Properly turn of the process (otherwise it creates multiple instances of all objects...)
            android.os.Process.killProcess(android.os.Process.myPid());
            System.exit(1);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

    }
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    loop.setRunning(true);
    loop.start();
}
}

2 个答案:

答案 0 :(得分:0)

你为什么要完成MenuScreen?只需启动主要活动,不要在代码中调用完成()。它会正常工作。

但你的代码错了!

而不是这段代码:

Intent i = new Intent(context, MenuScreen.class);
((Main)context).finish();
context.startActivity(i);

这样做:

Intent i = new Intent(context, MenuScreen.class);
context.startActivity(i);
((Main)context).finish();

希望这有帮助:)

答案 1 :(得分:0)

您正在完成您的活动,之后您尝试打开活动(没有活动时)。

替换此行..

 Intent i = new Intent(context, MenuScreen.class);
    ((Main)context).finish();
    context.startActivity(i);

用这个..

  Intent i = new Intent(context, MenuScreen.class);
  context.startActivity(i);
  ((Main)context).finish();