9

In my application. I have a login screen. If the login is successful, a tab activity will launch, in that there are 4 tabs. When I press one of the buttons in the tab, a new activity will launch. there is an event in my login class that will get fired in some cases. I want to go back to the tab activity when the event get fired. I have written a code using Intent. That code is working fine. But after reaching the tab activity I don't want to go back to the activity when back button is pressed. I want to remove this. I want to show login when Back is pressed. Is there any way to do this? This is the code I have used:

Intent tabi=new Intent(getApplicationContext(),Tab.class);
startActivity(tabi);

The code onkeydown in tab activity is:

    @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {


        super.onKeyDown(keyCode, event);
        return true;
    }
    return false;
}
Chintan Soni
  • 24,761
  • 25
  • 106
  • 174
irfan
  • 869
  • 3
  • 12
  • 25

5 Answers5

11
Intent tabi=new Intent(getApplicationContext(),Tab.class);
            startActivity(tabi);
finish();
Digvesh Patel
  • 6,503
  • 1
  • 20
  • 34
7

Just call finish(); method after starting the activity. It will prevent from back button

Like:

Intent tabi=new Intent(getApplicationContext(),Tab.class);
startActivity(tabi);
finish();
Shailendra Madda
  • 20,649
  • 15
  • 100
  • 138
4

use onBackPressed and in that start Your activity

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();
   Intent intent = new Intent(Tab.this , Login.class);
   startActivity(intent)
}

and you can clear the history with bellow code that user can't came back from login Page to visited Page

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Shayan Pourvatan
  • 11,898
  • 4
  • 42
  • 63
2

When event get fired, call tab activity with these intent flags

Intent tabi=new Intent(getApplicationContext(),Tab.class);
tabi.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(tabi);
Jomon KC
  • 55
  • 1
  • 7
1

Going back to login screen, can be achieved in two ways:

First way is, (assuming you are currently on login scren) to simply navigate to the next activity using Intent, without finishing the current activity, and inside onBackPressed() you simply need to call finish().

Second way is, if you finish() the login activity, then simply navigate to login activity using Intent and finish the tab activity.

Its simple. Use Intent to go to login screen:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {

    startActivity(new Intent(getApplicationContext(), Login.class));
    finish();

    super.onKeyDown(keyCode, event);
    return true;
}
return false;
}
Chintan Soni
  • 24,761
  • 25
  • 106
  • 174