Android: a great way to clear all activity on logout?

All actions in my applications with the "Exit" button and the user can exit any activity. I want to send the user to the system without showing previous activity. for this i use:

The following is a logout method.

public void logoutUser() { //clearing all data from sharedPreferences editor.clear(); editor.commit(); Intent intnt = new Intent(contxt, LoginActivity.class); // Closing all the Activities intnt.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // Add new Flag to start new Activity intnt.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK ); // Staring Login Activity contxt.startActivity(intnt); } 

from the second action, when the user presses the logout button, and then the call method is called. My second activity class extends the SherlockFragmentActivity property.

 public void Logout(MenuItem v) { sessionMngr.logoutUser(); } 

I am on the login screen when I press the exit button, but when I press the button on the device, it shows the previous steps - I have to go to the Android main screen when I press the "Back" button on the login screen.

I saw some question about stackoverflow, but did not reach my goal. someone said that I am using android: noHistory = "true" in the manifest file, but what happens when I am in the third step and press the button that displays the Android start screen, but it should go into the second activity. I also tried FLAG_ACTIVITY_NO_HISTORY, but that also does not reach my goal.

I do not understand where I am mistaken. Please someone have a solution.

Thank you in advance

+4
source share
11 answers

Try to specify both of them:

 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); 

Also, to be safe, call the finish () after starting this operation.

+12
source

The best way is to use LocalBrodCastManager in the application during logout.

When the user clicks the exit button, you can send a local broadcast using the code below.

 @Override public void onclick(View view){ //do this on logout button click final String LOG_OUT = "event_logout"; Intent intent = new Intent(LOG_OUT); //send the broadcast to all activities who are listening LocalBroadcastManager.getInstance(this).sendBroadcast(intent); } 

Now all activity should listen to this broadcast. And the action will look like

 @Override protected void onCreate(Bundle savedInstanceState) { // Register mMessageReceiver to receive messages. LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, new IntentFilter(LOG_OUT)); } // handler for received Intents for logout event private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { //do your code snippet here. finish(); } }; @Override protected void onDestroy() { // Unregister since the activity is not visible LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver); super.onDestroy(); } 

Note If you have more than one action, it is recommended to extend all activity from the base activity and implement this local broadcast manager only in the base action. So you only need to implement the exit code in one place.

+7
source

Here's how I solved it:

 Intent intent = new Intent(context, ActivityLogin.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent); 

Works with API> = 11

+2
source

Try FLAG_ACTIVITY_MULTIPLE_TASK

It should work just like you do now, is login activity root (luncher) activity?

You can try in the onResume method to do something like this

 if(!login) { finish(); } 

Looks good, but should work.

0
source

you can use onbackpressed in your login activity to determine what it should do when the key is pressed

 @Override public void onBackPressed() { //what it should do on back } 
0
source

In your activity, when you log out, click

  Button logout = (Button) findViewById(R.id.logout); logout.setOnClickListener(new OnClickListener() { Intent myIntent = new Intent(CurrentActivity.this, LoginActivity.class); myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);// clear back stack startActivity(myIntent); finish(); }); 

In LoginActivity, click the back button. If LoginActivity is MainActivity at the click of a button, the goto action will complete and the home screen.

0
source

This is work for me, try this ... and don't forget to unregisterReceiver fooobar.com/questions/28146 / ...

0
source

Adding the parameter below when starting LoginActivity will help you solve your problem.

intent.addFlags (Intent.FLAG_ACTIVITY_CLEAR_TOP);

A - Login

B - Some activity

C - Some activity

D - Some activity - exit from this activity and transition to input activity.

Above, the flag will clear the history of the event stack from memory. So you do not get another activity when you click

0
source
 Intent intent = new Intent(SourceActivity.this,DestinationActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); 

Use this code before moving on to login activity or to any assignment operation. In addition, all actions that are on the stack will be cleared.

Hope this helps you.

0
source

Hope this helps.

1) I used Builder to show the message after the user clicked the logout button.

2) As soon as the user clicks “Take me!”, He will return to the main page, and when you click the “Back” button on the device, he will not show the previous actions.

 logout=(Button)findViewById(R.id.btn_logout); logout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { final AlertDialog.Builder builder = new AlertDialog.Builder(EngagementActivity.this); builder.setTitle("Info"); builder.setMessage("Do you want to logout ??"); builder.setPositiveButton("Take me away!", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Intent intent = new Intent(EngagementActivity.this,MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); startActivity(intent); finish(); } }); builder.setNegativeButton("Not now", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { dialogInterface.dismiss(); } }); AlertDialog dialog = builder.create(); dialog.show(); } }); 
0
source

finishAffinity() works great for API levels finishAffinity()

0
source

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


All Articles