-5

I have Splash Activity => Login Activity => Main Activity.... Once user Login My user Should redirect To the Main Activity Directly till LOGOUT.....

Give me a specific solution please...What to do in which Activity..

I am working with web services....suggest me if SQ Lite require or Shared-preference Or Session.Class.....

Please BE SPECIFIC ...what to do in which Activity/Class...

Before Login..

Splash Activity => Login Activity => Main Activity

I want flow after LOGIN like this..

Splash Activity =>Main Activity ....

Thank You in advance.....

SplashActivity.java public class Splash extends Activity {

Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);

    Thread timerThread = new Thread(){
        public void run(){
            try{
                sleep(3000);
            }catch(InterruptedException e){
                e.printStackTrace();
            }finally{

                String sharedPrefId     = "MyAppPreference";
                SharedPreferences prefs = getSharedPreferences(sharedPrefId, 0);

                boolean isLoggedIn      = prefs.getBoolean("isLoggedIn", false);
                if(isLoggedIn)
                {
                    // Show Main Activity
                    Intent intent1= new Intent(Splash.this,SnetHome.class);
                    startActivity(intent1);
                }
                else
                {
                    // Show Login activity
                    Intent intent2= new Intent(Splash.this,Login.class);
                    startActivity(intent2);
                }
                //if{
                //if user redirect to LoginActivity
                //Intent intent = new Intent(Splash.this,Login.class);
                //startActivity(intent);
                //}else{
                     //otherwise redirect to SnetHome activity
                // }
            }
        }
    };
    timerThread.start();
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    finish();
}

}

Login.java

public class Login extends AppCompatActivity implements OnClickListener {

private EditText user, pass;
private Button mSubmit, mRegister;

// Progress Dialog
private ProgressDialog pDialog;

// JSON parser class
JSONParser jsonParser = new JSONParser();

// php login script location:

// localhost :
// testing on your device
// put your local ip instead, on windows, run CMD > ipconfig
// or in mac's terminal type ifconfig and look for the ip under en0 or en1
// private static final String LOGIN_URL =
// "http://xxx.xxx.x.x:1234/webservice/login.php";

// testing on Emulator:
private static final String LOGIN_URL = "http://192.168.1.106/SnetWebservice/login.php";

// testing from a real server:
// private static final String LOGIN_URL =
// "http://www.mybringback.com/webservice/login.php";

// JSON element ids from repsonse of php script:
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";

private Toolbar mToolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);


    //toolbar
    mToolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(mToolbar);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    // setup input fields
    user = (EditText) findViewById(R.id.username);
    pass = (EditText) findViewById(R.id.password);

    // setup buttons
    mSubmit = (Button) findViewById(R.id.login);
    mRegister = (Button) findViewById(R.id.register);

    // register listeners
    mSubmit.setOnClickListener(this);
    mRegister.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent inte = new Intent(Login.this, Register.class);
            startActivity(inte);
        }
    });

}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
        case R.id.login:
            new AttemptLogin().execute();
            break;
            /* case R.id.register:
            Intent i = new Intent(this, Register.class);
            startActivity(i);
            break;
            */
        default:
            break;
    }
}

class AttemptLogin extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(Login.this);
        pDialog.setMessage("Attempting login...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    @Override
    protected String doInBackground(String... args) {
        // TODO Auto-generated method stub
        // Check for success tag
        int success;
        String username = user.getText().toString();
        String password = pass.getText().toString();
        try {
            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("username", username));
            params.add(new BasicNameValuePair("password", password));

            Log.d("request!", "starting");
            // getting product details by making HTTP request
            JSONObject json = jsonParser.makeHttpRequest(LOGIN_URL, "POST",
                    params);

            // check your log for json response
            Log.d("Login attempt", json.toString());

            // json success tag
            success = json.getInt(TAG_SUCCESS);
            if (success == 1) {
                Log.d("Login Successful!", json.toString());
                // save user data
                SharedPreferences prefs = PreferenceManager
                        .getDefaultSharedPreferences(Login.this);
                Editor edit = prefs.edit();
                edit.putString("username", username);
                edit.commit();
                prefs.edit().putBoolean("isLoggedIn", true).commit();
                Intent i = new Intent(Login.this, SnetHome.class);
                finish();
                startActivity(i);
                return json.getString(TAG_MESSAGE);
            } else if (success == 0) {
                Log.d("Login Failure!", json.getString(TAG_MESSAGE));
                return json.getString(TAG_MESSAGE);
            }else {
                Log.d("Login Failure!", json.getString(TAG_MESSAGE));
                return json.getString(TAG_MESSAGE);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;

    }

    protected void onPostExecute(String file_url) {
        // dismiss the dialog once product deleted
        pDialog.dismiss();
        if (file_url != null) {
            Toast.makeText(Login.this, file_url, Toast.LENGTH_LONG).show();
        }

    }
}

}

  • 3
    Possible duplicate of [login once and skip login layout everytime app starts after first login](http://stackoverflow.com/questions/16538173/login-once-and-skip-login-layout-everytime-app-starts-after-first-login) – Nigam Patro Dec 18 '15 at 11:18
  • You can use `SharedPreference` for that. – Nigam Patro Dec 18 '15 at 11:19
  • Use SQlite in case of big data, for short information use preferences. – ravidl Dec 18 '15 at 11:20
  • @ravidl for this question, its not needed to use `SQLite` as per my knowledge. – Nigam Patro Dec 18 '15 at 11:24
  • @Nigam If he is fetching big data from web service then (as per my knowledge) keeping big data in shared preferences is not a good idea, that will take large cache memory. – ravidl Dec 18 '15 at 11:35
  • @ravidl Yes you are right, but also with that using `SharedPreference` for storing login status is better. Because it will be faster. – Nigam Patro Dec 18 '15 at 11:38
  • @NigamPatro that I mentioned already in the comment, for short information as like user preferences, conditional checks which are needed all time in hte app coulb be stored shared preferences. – ravidl Dec 18 '15 at 11:41
  • @ravidl OK. Thanks... – Nigam Patro Dec 18 '15 at 11:41
  • Guys Can u Give some Source Code ...I am beginner .. I am not Getting anything – Atif Patel Dec 19 '15 at 04:21

3 Answers3

0

Like this

onCreate of Spalsh Activity

if(isLogin)  //value comes from Shared Preference 
{
Go to Main 
}else 
{
Go to Login
}
Nisarg
  • 1,358
  • 14
  • 30
0

Use SharedPreference which will store a boolean variable as isUserLoggedIn if user has logged in then it will store true otherwise false and then check the value of SharedPreference at splash screen.

Pankaj
  • 7,908
  • 6
  • 42
  • 65
0

You can use SharedPreference for achieving this. You need to implement the logic in your SplashActivity. You need to check whether already logged in or not using the value stored in shared preference and show next activity based on that.

In your SplashActivity (Where you launch the login activity), add the logic like:

// Retrieving your app specific preference
String sharedPrefId     = "MyAppPreference";
SharedPreferences prefs = getSharedPreferences(sharedPrefId, 0);

boolean isLoggedIn      = prefs.getBoolean("isLoggedIn", false);
if(isLoggedIn)
{ 
    // Show Main Activity
}
else
{
    // Show Login activity
}

And in your LoginActivity, after successful login set the value to true:

prefs.edit().putBoolean("isLoggedIn", true).commit();
Midhun MP
  • 103,496
  • 31
  • 153
  • 200
  • Not Working :(.....I am editing My Question .. putting code inside that See it... – Atif Patel Dec 19 '15 at 04:28
  • @AtifPatel: Since you are using global shared preference and not your app specific one, you need to replace : `String sharedPrefId = "MyAppPreference"; SharedPreferences prefs = getSharedPreferences(sharedPrefId, 0);` with `SharedPreferences prefs = PreferenceManager .getDefaultSharedPreferences(Splash.this);` – Midhun MP Dec 19 '15 at 09:45
  • Salute you sir Thanks again You save my so much work and time – Atif Patel Dec 21 '15 at 05:06
  • help.......@Mithun Sir ... i wanna set Logout on HomePage ....How does it possible to Logout from global Shared Preference ...... – Atif Patel Dec 23 '15 at 05:23
  • @AtifPatel: You can clear the preference using `prefs.edit().remove("isLoggedIn").commit();` – Midhun MP Dec 23 '15 at 05:30
  • Thank you Sir ... Its Working i got ans – Atif Patel Dec 23 '15 at 11:37