0

I'm implementing Android Java application in which there are two types of User. Each of them have permission to use some of the application features but not all of them.

My current implementation includes setting of all components visibility inside one activity every time user is redirected to that same activity. Example:

protected void onCreate(Bundle savedInstanceState) {
    if(!userLoggedIn()) {
        // Set all visibilities
    } else if (loggedUserType() == UserType.USER1) {
        // Set all visibilities
    } else {
        // Set all visibilities
    }
}

Is there standard approach in android java applications that state how to deal with this issue? If not, is there any better approach than the one shown in example above?

1 Answers1

-1

you can have one fragment for each user type and show that fragment inside your activity. inside your activity layout add a container.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    //rest of activty views

    <FrameLayout 
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</RelativeLayout>

and based on user type create and add your fragment

FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ExampleFragment fragment;
if(!userLoggedIn()) {
    fragment = new ExampleFragment1();
} else if (loggedUserType() == UserType.USER1) {
    fragment = new ExampleFragment2();
} else {
    fragment = new ExampleFragment3 ();
}
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();

this way you will have clean architecture and can change each fragment separately

Farid
  • 1,024
  • 9
  • 16