I have two activities. In first I come to the second activity from first so:
Intent i = new Intent(this, SecondClass.class);
startActivity(i);
but special condition require that I restart first activity because I return to it with same way:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
//...
Intent i = new Intent(this, FirstClass.class);
startActivity(i);
super.onKeyDown(keyCode, event);
return true;
}
return super.onKeyDown(keyCode, event);
}
Because I can’t leave first activity:
If I use
@Override
public void onBackPressed()
{
finish();
System.exit(0);
return;
}
or
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
finish();
System.exit(0);
return super.onKeyDown(keyCode, event);
}
return super.onKeyDown(keyCode, event);
}
It close my application but open second activity.
Help please!
just call finish() after you start a new activity(and only after you start a new activity) and let android do the rest.
It will start the second activity and close the first one. If you press the back key now it will take you to the last open activity ( not necessarily one from your app )