user2728910 user2728910 - 5 months ago 22
Android Question

Activity.finish() finishes all activities

I have two Activities: MenuScreen and Main. MenuScreen starts Main and finishes himself.

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


However when i finish Main and start MenuScreen, it also finishes MenuScreen. Note that the finish is called in the SurfaceView of Main

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


But I just want to finish Main and go back to MenuScreen. What am I doing wrong?

Manifest:

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

}
}


Main:

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) {

}

}


Overview:

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

Answer

Why are you doing finish MenuScreen? Just start Main activities and do not call finish() in your code. It's will work fine.

But Your code is wrong!

Instead of this code:

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

Do this:

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

Hope this help :)

Comments