0
protected void onCreate(Bundle savedInstanceState) {
  //if user is already logged in open the profile activity directly
  if (SharedPrefManager.getInstance(this).isLoggedIn()) {
    finish();
    startActivity(new Intent(this, Home.class));
  }
  buttonSignIn.setOnClickListener(this);
  buttonSignUp.setOnClickListener(this);
}

Can someone please explain to me why is finish() called before launching the Home.class if the user is already logged in. I'm trying to go through some source code and not able to understand this.

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96

4 Answers4

0

After finish() you can't get context from the activity.

You have to start activity like this:

if (SharedPrefManager.getInstance(this).isLoggedIn()) {            
        startActivity(new Intent(this, Home.class));
        finish();
    }
Raja
  • 2,775
  • 2
  • 19
  • 31
0

simply when you call finish(); its more like an intent which do this :

Intent intent = new Intent(whereYouareActivity.this , mainActivity.class};
startActivity(intent);
White Druid
  • 295
  • 1
  • 12
0

when you finish an activity, the activity gets destroyed but not the process. This is an important aspect of Android programming and is important to understand how it's different from other platforms.

And you can finish your activity by adding these lines :-

if (SharedPrefManager.getInstance(this).isLoggedIn()) {            
        startActivity(new Intent(this, Home.class));
        finish();
    }
Ankita
  • 1,129
  • 1
  • 8
  • 15
0

By calling finish() you actively close your current Activity. This prevents your activity from being shown again when the user presses the back button.

Ombrax
  • 95
  • 7