The better solution is, you should replace startActivity() with startActivityForResult() for activity transition. And from logout userActivity you just set a result and you will get onActivityResult in your mainActivity if you handle it properly.Then you can simply finish mainActivity.
Refer https://developer.android.com/training/basics/intents/result.html
Another way is finish MainActivity from its onResume, if the activity onResume is called after logout from userActivity.
1
solved Finishing activities A & B from activity B and go to activity C in android