سؤال

I have a log in system in my app, and I would like to make it so that once the user logs into their account the back stack get's cleared, so they cannot see the log in activity, unless they click on the log out button in the main activity. How would I accomplish this?

any help would be greatly appreciated.

هل كانت مفيدة؟

المحلول

Use finish() method to destroy your login activity after intent. It'll be removed from stack as well.

Intent i = new Intent(this, YourAccountActivity.class);
startActivity(i);

// destroying your current activity
finish();

If you don't want to put this activity in the stack, you can also use

android:noHistory="true" in AndroidManifest.xml

When you change the activity, this activity won't be in the stack.

A value of "true" means that the activity will not leave a historical trace. It will not remain in the activity stack for the task, so the user will not be able to return to it.

Another option is to use IntentFlags.

Intent i = new Intent(this, YourAccountActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);

نصائح أخرى

If you want to manually finish two activity at a same time then you can use ApplicationContext class and in that define.

MyApplicationClass.java

public Activity homeActivity = null;
public Activity otherActivity = null;

and when you start Homeactivity you can do in onCreate() method :

HomeActivity.java

private MyApplicationClass myApplication = null;

   myApplication = (MyApplicationClass )getApplicationContext();

   myApplication.homeActivity = HomeActivity.this;

Same in otheractivity's onCreate() method:

otheractivity.java

private MyApplicationClass myApplication = null;

       myApplication = (MyApplicationClass )getApplicationContext();

       myApplication.otherActivity = otheractivity.this;

And when you want to finish Homeactivity and otheractivity at a same time then You can finish like this way..

myApplication.homeActivity.finish();
myApplication.otherActivity.finish();
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top