-4

Here is what I want to do, I have an android login form with username and password, after the user enters his credentials and login, the next form should display on top of the page welcome,+username entered from the login page! but if user revisited my app then the message should be welcome back username, and how i can know the user has visited again in my app?, can someone please help me??

I'm new to android development and don't know how to go about this. Thanks public class HomeScreen extends Activity implements OnClickListener { String response = null;

public static HomeScreen object = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    object = this;
    // String type = getResources().getString(R.string.TYPE);
    // Logger.logger("mobile type :::::::::::: " + type);
    // if (type.equalsIgnoreCase("mobile")) {
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    // }
    setContentView(R.layout.home);

    findViewById(R.id.btn_call_us).setOnClickListener(this);
    findViewById(R.id.btn_email_us).setOnClickListener(this);
    findViewById(R.id.btn_panel_book).setOnClickListener(this);
    findViewById(R.id.btn_get_instant_quote).setOnClickListener(this);
    findViewById(R.id.btn_logout).setOnClickListener(this);
    ((TextView) findViewById(R.id.tv_welcome_msg_title)).setText("Welcome "
            + Comman.getPreference(HomeScreen.this, AppConstants.PRE_F_NAME, "") + "!");

    new getJustInData().execute();
}

String response;

    @Override
    protected String doInBackground(String... params) {
        try {
            response = HttpProcess.postDataOnServer(AppConstants.URL_WELCOME + "client="
                    + Comman.getPreference(HomeScreen.this, AppConstants.PRE_COMPANY_NAME, ""));
            Logger.logger("respons in welcome Message : " + response);
        } catch (Exception e) {
            response = "";
        }
        return "";
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        PDialog.dismiss();
        String msg = Comman.getPreference(HomeScreen.this, AppConstants.PRE_WELCOME, "");

        try {
            String WelComeMsgResponseList = JsonParser.readWelcomeResponse(response);
            if (WelComeMsgResponseList != null && WelComeMsgResponseList.length() > 0) {
                Comman.setPreference(HomeScreen.this, AppConstants.PRE_WELCOME, WelComeMsgResponseList);
                ((WebView) findViewById(R.id.webview)).loadData("<font style='color:#ffffff;'><MARQUEE> "
                        + WelComeMsgResponseList + "  </MARQUEE></font>", "text/html", null);
                ((WebView) findViewById(R.id.webview)).setBackgroundColor(Color.BLACK);
                return;
            }
        } catch (Exception e) {
        } catch (Error e) {
        }
        ((WebView) findViewById(R.id.webview)).loadData("<font style='color:#ffffff;'><MARQUEE> " + msg
                + " </MARQUEE></font>", "text/html", null);
        ((WebView) findViewById(R.id.webview)).setBackgroundColor(Color.BLACK);
    }

}

}

2 Answers2

1

Save your data in ShreadPreference and handle using your logic.

d.k.
  • 415
  • 4
  • 16
  • you can keep revisit_Count in it as zero initially. When user come to welcome + usrname then set to 1. So when you'll come to this page again then check that count if it is 1 then show welcome back message otherwise welcome. – d.k. Oct 27 '15 at 10:14
  • Or maybe just a boolean, he just wants to know if it's going to be the first time, the user access the app – Simone Leoni Oct 27 '15 at 10:17
  • i shared my code can any1 please tell me what should i do there? – Gurleen kaur Oct 27 '15 at 10:41
  • check here that: String message = ""; if(Comman.getPreference(HomeScreen.this, AppConstants.IS_REVISIT, false)){ message = "Welcome back " + Comman.getPreference(HomeScreen.this, AppConstants.PRE_F_NAME, "") + "!" }else{ message = "Welcome " + Comman.getPreference(HomeScreen.this, AppConstants.PRE_F_NAME, "") + "!" Comman.setPreference(AppConstants.IS_REVISIT, true) } ((TextView) findViewById(R.id.tv_welcome_msg_title)).setText(message); – d.k. Oct 27 '15 at 10:46
  • You will have to take an key(IS_REVISIT) in shared preference which will be fasle initially. Is it good for you? – d.k. Oct 27 '15 at 10:51
0

Useshared preference for username and also store login status. e.g -> on starting application check user login status if true show user name in top of the activity else redirected to login page.

private static final String PREFER_NAME = "";

private static final String IS_USER_LOGIN = "";

public static final String KEY_NAME = "";

public static final String KEY_Password = "";
SharedPreferences pref;
SharedPreferences.Editor editor;

in create method put these lines:

    pref = context.getSharedPreferences(PREFER_NAME, PRIVATE_MODE);
    editor = pref.edit();

    editor.putBoolean(IS_USER_LOGIN, true);

    editor.putString(KEY_NAME, name);

    editor.putString(KEY_Password, password);

    editor.commit();

check if user is logged in:

public boolean isUserLoggedIn() {

    return pref.getBoolean(IS_USER_LOGIN, false);
}
Hcorg
  • 11,598
  • 3
  • 31
  • 36
Muhammad Fahad
  • 101
  • 2
  • 10