Hide login activity

I have a LoginActivity that checks SharedPreferences for login details, then redirects to HomeActivity and other actions after that. I placed the β€œExit” menu item in each of these actions and used this code on the exit button.

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

How to hide login activity, so when the user clicks the "Back" button on the main screen, he will close the application.

As when opening the application, it shows the initial screen, and when I click the button, I usually close the application. But in my case, it takes me to the login screen, which is the first screen that checks the user credentials.

I cannot end the login activity, otherwise this solution will not work.

I am new to Android. Please suggest something to solve this problem.

+6
source share
3 answers

you should try this on your home Back Back function:

 onBackpress(){ Intent intent = new Intent(mContext, LoginActivity.class); intent.putExtra("FLAG", 0); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); startActivity(intent); } 

and in your LoginActivity just do:

  onNewIntent(Intent intent){ int i = intent.getIntExtra("FLAG", 0); if(i == 0) finish(); } 

remember launchMode for activity in the manifest must be singleTop.

+5
source

Cancel the Activity.onBackPressed () method, and then send the application via Intent home.

From the SDK:

Hope with the following categories will allow you to return home.

ACTION_MAIN with category CATEGORY_HOME - Launch the main screen.

+4
source

You can override the event of pressing the "Back" button and close the application when pressed.

+1
source

Source: https://habr.com/ru/post/907742/


All Articles