Reputation: 869
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;
}
Upvotes: 9
Views: 13285
Reputation: 55
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);
Upvotes: 2
Reputation: 21531
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();
Upvotes: 7
Reputation: 25267
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;
}
Upvotes: 1
Reputation: 11948
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);
Upvotes: 4
Reputation: 6533
Intent tabi=new Intent(getApplicationContext(),Tab.class);
startActivity(tabi);
finish();
Upvotes: 11