Question

is it possible to go back to a specific activity when the hard key back button is pressed enter image description here? Instead of going back to the last activity go back two activitys?

Was it helpful?

Solution

1.If you know which is the first activity, just start it by overiding the onBackPressed().

@Override
public void onBackPressed() {
   Intent intent = new Intent(Third.this,First.class);
   startActivity(intent);
}

2.You can also call finish() immediately after you start Third activity. So that on back pressed, will go back to the first activity.

Intent intent = new Intent(Second.this, Third.class);
startActivity(intent);
finish();

3.Prevent the second activity from being added to history stack.

Intent intent = new Intent(First.this, Second.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);

OTHER TIPS

Yes It is possible... You can fire the Intent of specified activity in the following methods.

Use this method

 @Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();
}

Or you can use

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)  {
    if (Integer.parseInt(android.os.Build.VERSION.SDK) < 5
            && keyCode == KeyEvent.KEYCODE_BACK
            && event.getRepeatCount() == 0) {
        Log.d("CDA", "onKeyDown Called");

        onBackPressed();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

You can start your latest activity with startActivityForResult and set the flag before finishing that activity, and in the "parent" activity override the onActivityResult(int, int, Intent) method and you can finish that activity in the onActivityResult method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top