EDITED WITH ONE (HALF) SOLUTION AT THE END OF THE POST

I have the next logic in my **`MainActivity.class`**: 

```
if(SaveSharePreferences.getUserEmail().length == 0) {
    Intent intent = new Intent(MainActivity.this, LoginActivity.class);
    startActivity(intent);
    finish(); //Closing MainActivity
} else {
    Intent intent2 = new Intent(MainActivity.this, MainMenu.class);
    startActivity(intent2);
    finish(); //Closing MainActivity
}

```

So in the **`LoginActivity.class`**:

```
...
public void login() {
    Intent intent = new Intent(LoginActivity.this, MainMenu.class);
    startActivity(intent);
    finish(); //Closing LoginActivity
}
...

```

The main problem to handle the `Activities` is that my `MainMenu.class` contains a `ViewPager` with three tabs, and is in this different tabs (`Fragments`) where I choose go to one deeper `Activity` or another.

In one of these Fragments I have the **`ProfileActivity.class`** option, where I logout. The code is:

```
public void logOut() {    
    Intent intent = new Intent(ProfileActivity.this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    SaveSharedPreference.clearUser(ProfileActivity.this);
    startActivity(intent);
    finish();
}

```

So it is supposed to be that my activities stack is empty, or it just contains the `MainActivity.class` after the logout, but the next time I do login, the `MainMenu.class` is opened two, three, four times, depending on the current time I´ve logged in.

I´ve tried with `intent.addCategory(Intent.CATEGORY_HOME)`, also declaring: 

```
public class MainMenu extends AppCompatActivity {
    ...
    static Activity mainMenuActivity;
    ...
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
        mainMenuActivity = this;
        ...
    }
    ...
}

```

and then closing it from the others, but nothing works. Any solution?

---

**TEMPORAL SOLUTION**

The `Flags` are not working properly either declaring a `public static Activity` and then closing it manually, so I´ve edited the **AndroidManifest.xml** on the following way:

```
<activity
    android:name=".NameOfTheActivity"
    android:launchMode="singleTask">

```

What this `launchMode="sigleTask"` does?

> 
> The system creates the activity at the root of a new task and routes
>  the intent to it. However, if an instance of the activity already
>  exists, the system routes the intent to existing instance through a
>  call to its onNewIntent() method, rather than creating a new one.
> 
> 
> 

I´m avoiding the multiple opening problem that I had before. But some of the activities remain opened and I´m not able to close it, so this is not a definitive solution.