我有一个应用程序,其中我有Login活动和另一个名为MainActivity的活动,它添加了两个片段,条件是当用户成功登录然后Mainactivity来了。
这里我要显示"登录成功消息"只有一次,因为当用户登录成功时,用户将永远不会看到登录活动。我该怎么做
这是登录活动的代码: -
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 ////
}
主要活动代码
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();
}
答案 0 :(得分:1)
一种方法是使用Bundle。 在您的登录功能中,请在此处更改:
Intent i = new Intent(CLoginScreen.this, CMainActivity.class);
i.putExtra("login",true);
startActivity(i);
在您的主要活动中,在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
}
答案 1 :(得分:0)
将if条件放在登录页面的顶部,这将检查prefrence值。如果它会找到您保存的登录参数,那么它将通过意图传递直接重定向到主活动,如下所示。否则它将继续登录屏幕。
如果你想显示成功登录的消息,那么你可以写下面的代码。
在LoginActivity上 Toast.makeToast(contxt,"YOUR_MESSAGE",Toast.LENGTH_SHORT).show();
,然后重定向到MainActivity。
Intent intent = new Intent(this,MainActivity.class);
intent.addFlag(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlag(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
此处Intent.FLAG_ACTIVITY_CLEAR_TOP
和Intent.FLAG_ACTIVITY_CLEAR_TASK
将清除活动堆栈。在下次申请开始之前,你永远不会进入登录界面。
如果你想在主要活动中显示消息,那么只需添加有意图的额外内容并通过getIntent().getStringExtra("loginParam");
如果您觉得有用,请标记为已阅读。