如何在Android中打开的应用程序上运行代码?
我的 Android 应用程序需要用户创建一个帐户才能使用该应用程序。帐户信息存储在 SQLite 数据库中。当应用程序启动时,我检查用户是否有帐户,如果没有,我会显示用户的注册活动。
现在,我收到用户的报告,他们有时会参加注册活动,即使他们已经创建了帐户。当他们关闭应用程序并再次重新打开它时,就会发生这种情况。
这是我正在使用的代码,我需要弄清楚问题可能是什么:
//MyApplication.java
public class MyApplication extends Application {
private DataBaseUtility dbu;
public boolean hasAccount;
@Override
public void onCreate() {
super.onCreate();
//Init sqlite database
this.dbu = new DataBaseUtility(this);
//This loads the account data from the database and returns true if the user has already created an account
this.hasAccount = loadAccount();
}
public boolean loadAccount() {
boolean loadedData = false;
String query = "SELECT data FROM tblaccount WHERE tblaccount.deleted=0";
Cursor cursor = this.dbu.getCursor(query);
if (cursor != null) {
while (cursor.moveToNext()) {
loadedData = true;
}
cursor.close();
}
return loadedData;
}
}
//MainActivity.java
public class MainActivity extends TabActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MyApplication application = (MyApplication)getApplication();
if (!application.hasAccount) {
//Take the user to the sign up activity
}
}
我的想法是,有时 MainActivity.onCreate()
在 MyApplication.onCreate()
之前运行代码>.可以这样吗?
My Android app need the user to create an account to be able to use the app. The account info is stored in SQLite database. When the application starts I check if the user has an account, if not I show a sign up activity for the user.
Now I get reports from users that they sometimes comes to the sign up activity even if they've already created an account. This happens when they've closed the application and reopen it again.
This is the code I'm using and I need to figure out what the problem might be:
//MyApplication.java
public class MyApplication extends Application {
private DataBaseUtility dbu;
public boolean hasAccount;
@Override
public void onCreate() {
super.onCreate();
//Init sqlite database
this.dbu = new DataBaseUtility(this);
//This loads the account data from the database and returns true if the user has already created an account
this.hasAccount = loadAccount();
}
public boolean loadAccount() {
boolean loadedData = false;
String query = "SELECT data FROM tblaccount WHERE tblaccount.deleted=0";
Cursor cursor = this.dbu.getCursor(query);
if (cursor != null) {
while (cursor.moveToNext()) {
loadedData = true;
}
cursor.close();
}
return loadedData;
}
}
//MainActivity.java
public class MainActivity extends TabActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MyApplication application = (MyApplication)getApplication();
if (!application.hasAccount) {
//Take the user to the sign up activity
}
}
My idea is that maybe sometimes MainActivity.onCreate()
runs before MyApplication.onCreate()
. Can that be the case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
application
的onCreate
中,您将检查用户是否有帐户并设置布尔值。您正在通过
application
的布尔值检查MainActivity
的onCreate
用户是否拥有帐户。application
的onCreate()
在MainActivity
的onCreate()
之前执行始终 案例!不可能出现不同的执行路径,并且由于application
的onCreate()
没有Runnable
,因此它是 100% 保证。请确保您的
DataBaseUtility
没有任何 Runnables。无论如何仍然有几种方法可以重现错误!我现在不会陈述这些,但当您看到:
SOLUTION
MainActivity
You have忘记更新application.hasAccount
成功注册后, 您就可以知道它们~为了避免数据库异常,
我使用这个:
备注 为数据库使用更强大的持久状态保存会更好 - 即
SharedPreferences
In
application
'sonCreate
, you are checking if the user has an account and setting a boolean.You are checking in the
MainActivity
'sonCreate
if the user has an account through theapplication
's boolean.application
'sonCreate()
executing beforeMainActivity
'sonCreate()
is always the case! It is impossible for a different execution path to occur and sinceapplication
'sonCreate()
does not have aRunnable
it is a 100% garantuee.Please make sure you're
DataBaseUtility
does not have any Runnables.Anyway STILL there are several ways to reproduce the error! I will not state these now but you can know them when you see:
SOLUTION
MainActivity
You have forgotten to updateapplication.hasAccount
upon successfull sign up~To avoid database exceptions
I use this:
REMARK It would be much better to use more strong persistent status saving for the database -i.e.
SharedPreferences