I am writing a simple application where the mainActivity invokes a secondActivity. Is there a method I can use to prevent the user from simply pressing back to return to the calling activity?
Start your Second activity this way:
Intent secActivity = new Intent(getApplicationContext(),
secActivity.class);
startActivity(secActivity);
MainActivity.this.finish();
If you wanted to prevent the user to ever go back to the mainActivity
, you should call the secondActivity
like this:
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
That way pressing back will exit your application.
If you want to block back hardware key, put this to your SecondActivity:
@Override
public void onBackPressed() {
// Blocks back button
}
If you want to close your FirstActivity so that users wont be able to visit it again, start your SecondActivity like this:
Intent i = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(i);
FirstActivity.this.finish();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.