-4

I am trying to create a screen for my app that allows the user to create a user name, and then launch the application. I already have a screen that has everything I want the user to do, but I now would like to add a login screen that the user must utilize before getting to the activity screen. I have the login screen designed, and a separate class ready to go. Doe's anyone know how to go about it so that MainActivity loads the login first? I assume that I would then need to set some sort of Boolean flag that allows Main to load the rest of the app?

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login);
    setContentView(R.layout.activity_main);

    Size = (SeekBar)findViewById(R.id.size);
     Speed =(SeekBar)findViewById(R.id.speed);
     Agility=(SeekBar)findViewById(R.id.agility);
     Vision =(SeekBar)findViewById(R.id.vision);


    text = (TextView) findViewById(R.id.text);
    Size.setOnSeekBarChangeListener(this);
    Speed.setOnSeekBarChangeListener(this);
    Agility.setOnSeekBarChangeListener(this);
    Vision.setOnSeekBarChangeListener(this);

}

I have a set content view for my login.

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/login"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

    <Button
        android:text="Begin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/begin"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="124dp" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:text="Name"
        android:ems="10"
        android:layout_above="@+id/begin"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginRight="88dp"
        android:layout_marginEnd="88dp"
        android:layout_marginBottom="149dp"
        android:id="@+id/editText2" />

</RelativeLayout>

I have my layout. I also have the class, which is empty for right now.

Am I going to have to do something inside my AndroidManifest?

John Casey
  • 27
  • 2
  • 8

2 Answers2

0

Currently Your MainActivity is the MAIN LAUNCHER activity. Change it to your LogInACtivity then your LogInActivity will launch first. In your Manifest cut the following codes from MainActivity's activity tag and paste it to your newly created LogInActivity's activity tag;

<intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

Hope this helps.

tahsinRupam
  • 6,325
  • 1
  • 18
  • 34
0

You could try launching either an activity, or an activity for a result (the username) based off of a boolean SharedPreference. So, launch the MainActivity like normal, and in onCreate(), do a check to see if the user has logged in with their username. If not, show the login screen. When the user logs in, change the SharedPreference value.

Code

// In onCreate()...
SharedPreferences prefs = PreferenceManager
    .getDefaultSharedPreferences(StartActivity.this);

if (!prefs.getBoolean("LoggedIn", false)) {
    // Launch login activity
} else {
    // Do something else
}

// In your Login Activity, preferably after the user has provided a valid username...

SharedPreferences prefs = PreferenceManager
    .getDefaultSharedPreferences(StartActivity.this);
SharedPreferences.Editor editor = prefs.edit();

// Set LoggedIn to true
editor.putBoolean("LoggedIn", true);
editor.putString("username", <The username they chose>);

Hope this helps! You're on the right track in terms of the whole boolean thing, but just checking a locally declared variable won't cut it. You need to actually store data on the system to check it later, like during consecutive launches.

Community
  • 1
  • 1
Andrew Quebe
  • 2,263
  • 5
  • 25
  • 53