vishwas vishwas - 5 months ago 30
Android Question

How to show "success Login" in another activity in android?

I have an ap in which I have Login activity and have 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 becuse 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();

}

Answer

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
}