0

I have an app in which I have Login activity and another activity called MainActivity which add two fragment,condition is that when user success login then Mainactivity comes.

Here I want to show "login success message" only one time because when user login successfully then user will never see login activity.How can I do that

here is code for login activity :-

if (response.getString("resultdescription").equalsIgnoreCase("Transaction Successful")) {
                        s_szResponseMobile = response.getString("agentcode").trim();// get mobile number from server response
                        s_szResponsePassword = response.getString("pin").trim();// get password from server response
                        m_oLoginSession.setLoginData(s_szResponseMobile, s_szResponsePassword);// set response from to Login session...
                        // snack bar to notice user about changes...
                        try {
                            Intent i = new Intent(CLoginScreen.this, CMainActivity.class);
                            startActivity(i);
                        } catch (Exception e) {
                            // change View to CDealListing on successful login...
                            e.printStackTrace();
                        }
                        // if mobile number not edit ////
                    }

code for mainactivity

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Log.i(TAG, "onCreate..........");
    IntentFilter m_intentFilter = new IntentFilter();// creating object of Intentfilter class user for defining permission
    m_intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");// action to check Internet connection
    getApplicationContext().registerReceiver(m_oInternetChecker, m_intentFilter);// register receiver....
    init();

}
Hitesh Sahu
  • 41,955
  • 17
  • 205
  • 154
vishwas
  • 251
  • 1
  • 3
  • 16
  • Possible duplicate of [How to display login screen only one time?](http://stackoverflow.com/questions/9964480/how-to-display-login-screen-only-one-time) – Hitesh Sahu Jul 05 '16 at 03:33
  • Commenter Hitesh Sahu is right. That will solve your matters. I would only want to put some light at the **apply** versus **commit** aspect. Relevant: http://stackoverflow.com/a/5960732/5885018 – statosdotcom Jul 05 '16 at 03:38
  • You don't understand my question – vishwas Jul 05 '16 at 03:44
  • first login screen is visible to user when user enter his detail and tap on submit button his details are sent to server and server respond with "success" on success user will be navigate to Mainactivity I want to show"Login success" in Mainactivity only once how can I do that – vishwas Jul 05 '16 at 03:45

2 Answers2

1

One approach is using Bundle. In your login function,change here:

   Intent i = new Intent(CLoginScreen.this, CMainActivity.class);
   i.putExtra("login",true);
   startActivity(i);

In your Main Activity, in OnCreate :

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(TAG, "onCreate..........");
//check whether we have such data in our intent
if(getIntent().hasExtra("login")&&getIntent().getExtras().getBoolean("login")){
     // YOU COME FROM LOGIN SCREEN
}
Omid Heshmatinia
  • 5,089
  • 2
  • 35
  • 50
0

Put if condition on top of login page which will check prefrence value. and if it will find login perameters save by you then it will directly redirected to main activity by intent passing as shown below. else it will continue with login screen.

if you want to show message of successfull login then you can write below code.

Toast.makeToast(contxt,"YOUR_MESSAGE",Toast.LENGTH_SHORT).show(); on LoginActivity and then redirect to MainActivity.

Intent intent = new Intent(this,MainActivity.class);
intent.addFlag(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlag(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);

here Intent.FLAG_ACTIVITY_CLEAR_TOP and Intent.FLAG_ACTIVITY_CLEAR_TASK will clear activity stack. and you will never come in login screen until application start next time.

And if you want to show message in main activity then simply add extras with intent and get that in main activity by getIntent().getStringExtra("loginParam");

if you find it helpful then please mark as read.

ErShani
  • 392
  • 2
  • 9