0

Right now I am trying to implement different main activity of the app depending on the role of the user that is stored at the server-side database. Upon login it is returned to device.

Here Single application with different UIs depending on the user's role there was a similar topic to what I would like to achieve, but it did not offer what I wanted to achieve.

What I do is loading the login activity in MainActivity's onCreate(). After that I check the role and depending on this I use:

if (some condition here){
    //here I set the activity_main.xml
} else if (condition again) {
    //another variant of activity_main.xml
}

Is this the correct variant to do this? If not, are there any other ways to achieve this? By the way, when I connect user interface elements to xml's values outside of the corresponding conditional block, I keep getting a NullPointerException later on, even if I use the login credentials for the most privileged user, whose interface has the maximum elements (names of views are the same in all xml layouts, files only differ in number of elements).

Sure thing, I know I will be advised to use the login screen as the main one of my app (like the link above suggested), but the app is not going to require login every time it is started, and thus I do not really understand how to realise all this stuff.

Any help is appreciated.

Community
  • 1
  • 1
Rebellious
  • 18
  • 2
  • use different init() methods to set layout and initialise them on ur oncreate – Ak9637 Jan 21 '17 at 08:43
  • is the difference is on MainActivity or MainActivity's view? Because you can inflate different layout or fragment for different user's previleges – aldok Jan 21 '17 at 08:45

2 Answers2

1

You can inflate different layouts based on the conditions in your onCreate(): setContentView(R.layout.your_layout);. Make sure not to initialize widgets that you are not using in the layout though, as that can cause errors.

More complete example:

@Override
protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (condition1)
        setContentView(R.layout.your_layout1);
    else
        setContentView(R.layout.your_layout2);
}

I'd also recommend leaving an else condition in the event something goes wrong, so it loads a layout rather than crash your application.

Aubtin Samai
  • 1,281
  • 13
  • 24
  • 1
    What you typed is what I actually do. I thought this was not a correct approach, as I kept getting NullPointerException when initializing views from the activity file loaded in the conditional code block. Thanks for your hint. – Rebellious Jan 21 '17 at 14:30
0

My suggestion would be , Suppose you are having **activity_main.xml** and **activity_main_two.xml** as your 2 xml files each having different roles , check the the password and the username in the Login section to verify which kind of the user has logged in , then afterwards you make your logic.

Suppose you have two users in the company Managers and Employees

E,g like this pseudo code below:

if (user_is_an_executive){
 setContentView(R.layout.activity_main.xml);
}
else if (user_is_another_employee){
setContentView(R.layout.activity_main_two.xml );
}

NOTE:

Place this in the onCreate Method

Lutaaya Huzaifah Idris
  • 3,596
  • 8
  • 38
  • 77