I have the same constellation in the app I'm developing at the moment.
First of all you need a launching Activity extending FragmantActivity or AppCompatActivity. Let's call this activity MainActivity.
Just create another fragment for the login/registration with its layout.
And in the MainActivity, which is the launching activity, you can handle in the onResume()
-Method, whether to show the login fragment or not.
Example
@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume");
if (!userManager.isLoggedIn()) {
LoginFragment loginFragment = LoginFragment.newInstance();
//LoginFragment loginFragment = new LoginFragment();
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_layout, loginFragment,
LoginFragment.class.getSimpleName()
).commit();
}
}
userManager is an object of a service class for authentication. It has a method returning a boolean indication whether the user is logged in or not.
If the user is logged in, the current fragment is replaced by the login fragment.
That's all.
With fragments you are able to display different layouts conditionally with its own logic.
Explanation of instantiating the fragment:
If you add a fragment to your project, AndroidStudio creates some boilerplate code with a static factory method newInstance(), because the constructor mustn't be overloaded. If you don't need to pass arguments for the creation of the login fragment you can do it with new LoginFragment().
But if you need arguments, you should use the factory method newInstance, which you instantiate the fragment without any arguments.
Here is a good explanation for that:
https://stackoverflow.com/a/9245510/474985