3

I want to open login_activity on first time entering app, and then on the second entering to app open main_activity.

I create something but it wont work. so I wonder what I'm doing wrong? this is my LoginActivity

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

        userName = (EditText) findViewById(R.id.username);
        userPhone = (EditText) findViewById(R.id.userPhone);
        loginBtn = (Button) findViewById(R.id.buttonLogin);

        dbHandler = new LogsDBHandler(this);

        loginBtn.setOnClickListener(this);
        setTitle("AMS - biomasa | prijava");

       SharedPreferences pref = getSharedPreferences("ActivityPREF", Context.MODE_PRIVATE);
        if (pref.getBoolean("activity_executed", false)) {
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
            finish();
        } else {
            SharedPreferences.Editor edt = pref.edit();
            edt.putBoolean("activity_executed", true);
            edt.commit();
        }
    }

    public void insert() {
        User user = new User (
                userName.getText().toString(),
                userPhone.getText().toString());
        dbHandler.addUser(user);
        Toast.makeText(getBaseContext(), "Prijavljeni ste!", Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onClick(View v) {
        if (v == loginBtn && validateUser()) {
            insert();
        }
    }

In main activity i have only image and two buttons. And in manifest I add launcher to main and login activity.

<activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"></action>
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    <activity android:name=".LoginActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"></action>
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

What am I doing wrong here?

RubyDigger19
  • 835
  • 13
  • 38
  • 3
    Possible duplicate of [How to display login screen only one time?](http://stackoverflow.com/questions/9964480/how-to-display-login-screen-only-one-time) – Jas Mar 02 '16 at 09:50
  • use sharedprefrence...put flag on successful login in it and each next time check flag and load activity accordingly – H Raval Mar 02 '16 at 09:54
  • you should remove the launcher in mainActivity, and I also have a doubt, you are assigning true on the value even if the user didn't click login, but it's a detail.. anyway kevz answer is the best i think – Pier Giorgio Misley Mar 02 '16 at 10:04

4 Answers4

11
  1. Create one start-up activity call it as SplashActivity

    public class SplashActivity extends Activity{
    
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
    
    // decide here whether to navigate to Login or Main Activity 
    
        SharedPreferences pref = getSharedPreferences("ActivityPREF", Context.MODE_PRIVATE);
        if (pref.getBoolean("activity_executed", false)) {
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
            finish();
        } else {
            Intent intent = new Intent(this, LoginActivity.class);
            startActivity(intent);
            finish();
        }
    }
    
    }
    
  2. In your LoginActivity simply set activity_executed to true

    public void insert() {
        User user = new User (
            userName.getText().toString(),
            userPhone.getText().toString());
        dbHandler.addUser(user);
        Toast.makeText(getBaseContext(), "Prijavljeni ste!", Toast.LENGTH_SHORT).show();
    
    //set activity_executed inside insert() method.
    SharedPreferences pref = getSharedPreferences("ActivityPREF", Context.MODE_PRIVATE);
    SharedPreferences.Editor edt = pref.edit();
    edt.putBoolean("activity_executed", true);
    edt.commit();
    
    }
    

change manifest as below-

<activity android:name=".MainActivity"/>

<activity android:name=".LoginActivity" />

<activity android:name=".SplashActivity" >

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

</activity>
kevz
  • 2,727
  • 14
  • 39
  • This is good but somehow it always opens me LoginActivity when I'm entering the app. Could you possibly know why is this? Or I do something wrong.. – RubyDigger19 Mar 02 '16 at 10:32
  • @RubyDigger19: The First activity with Launcher "Intent Filter" is started. In your case it should be MainActivity and not LoginActivity. Is the sequence correct i.e MainActivity then LoginActivity in manifest file? – kevz Mar 02 '16 at 11:28
  • I did everything you say. Even when I put MainActivity to open first it doesn't open before that LoginActivity. But when the LOginActivity is the first, it opens it every time when I'm entering the app. – RubyDigger19 Mar 02 '16 at 14:22
  • @RubyDigger19: would u like to talk about it in chat? Here is the link http://chat.stackoverflow.com/rooms/105212/show-login-activity-only-once-and-on-next-start-main-activity – kevz Mar 03 '16 at 04:52
1

you can change launcher activity as main activity.so that when you open the application it is starting from main activity there you can check whether he is logged in or not.if he is not logged in you must navigate him to login activity or else you just do it as it is.Following is manifest file..

<activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN"></action> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".LoginActivity"></activity>

malli
  • 642
  • 2
  • 12
  • 30
0

You should add another empty activity (with no UI) that loads before anything.

Then use SharedPreferences to store some value. Thus if the user has already opened your app once, the value is stored. And then use a condition to check this value. If its the value you saved skip login_activity and direct to main_activity else direct to login_activity.

Ritesh Shakya
  • 575
  • 5
  • 17
0

Problem about the line

if (pref.getBoolean("activity_executed", false)) {

You can Implement this method to call inside if(appIsLoggedIn)

public boolean appIsLoggedIn(){
      return pref.getBoolean("activity_executed", false);
}
rmammadli
  • 76
  • 1
  • 7