1

I have an application which has a main activity. When it switches to a second activity I do it like this :

startActivity(intent);
finish();

I was hoping this would fully prevent the application from going back to the main activity. When I press the android back button in the secnond activity it goes back to the home screen (which is what I want). But when I open the app again from multitasking it goes back to the first activity! Has anyone run into this issue before?

BMARX123
  • 91
  • 10

2 Answers2

1

I was running into this problem, where my first screen would be a login activity that's only meant to show if the user is not logged in. Otherwise, it show the home screen. I tried out SharedPreferences for login persistence, but it turns out the authentication service I'm using, Google Firebase, has built-in login persistence.

Without more information, however, I can only suggest that you research SharedPreferences and have a boolean check a condition upon start-up. If the condition is met, start the home activity; otherwise, show the main activity. Something like:

boolean isLogin;
SharedPreferences settings;
SharedPreferences.Editor editor;

settings = getApplicationContext().getSharedPreferences("MyPrefs", MODE_PRIVATE);
isLogin = settings.getBoolean("islogin", false);

if(isLogin) {
     finish();
     goToDash();
}

Public Interface Shared Preferences, Android Developers

Obtaining Shared Preferences

Working with Android Shared Preferences

0

Figured it out! My application uses a twitter api, and relaunching the application would cause an error as it retried to get credentials it didn't have before. I also made sure to completely clear the stack when switching certain applications by doing:

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

BMARX123
  • 91
  • 10