在 OnCreate 中启动新的 Activity
我得到了基于 2 个活动的 Android 应用程序。 Main 活动只是一个输入字段和一个获取一些用户信息的按钮。当用户按下按钮时,条形码扫描仪 (ZXING) 将启动。
每件事都很完美。但知道我尝试检查 onCreate 是否已知用户信息。如果属实的话->启动条形码扫描仪。但条形码扫描仪活动似乎启动了两次,因为: 按一次后退按钮:另一个条形码扫描仪将被激活。 按两次后退按钮:主要活动将被激活。
这是 onCreate 中的检查:
if(pref.length() == 6){
startActivityForResult(intent, 0);
}
这是单击按钮时调用的函数:
public void onClick(View view) {
switch (view.getId()) {
case R.id.button1:
final EditText edit = (EditText) findViewById(R.id.panelID);
if(edit.getText().toString().length() == 6){
String temp = edit.getText().toString();
Log.e("click", temp);
Editor e = this.getPreferences(Context.MODE_PRIVATE).edit();
e.putString("panelID", temp);
e.commit();
startActivityForResult(intent, 0);
} else {
Toast.makeText(this, "Ongeldige invoer (6 cijfer id)", Toast.LENGTH_LONG).show();
}
break;
}
}
我做错了什么?
I got android app based on 2 activities. The Main activity is just a input field and a button to get some user information. When the user press the button a barcodescanner (ZXING) will start.
Every thing works perfect. But know i try to check in the onCreate if user information is already known. If its true -> start the barcodescanner. But it looks like the barcodescanner activity starts up twice, because:
pressing back button once: annother barcodescanner wil be active.
pressing back button twice: Main activty will be active.
this is the check inside onCreate:
if(pref.length() == 6){
startActivityForResult(intent, 0);
}
and this is the function called when clicking the button:
public void onClick(View view) {
switch (view.getId()) {
case R.id.button1:
final EditText edit = (EditText) findViewById(R.id.panelID);
if(edit.getText().toString().length() == 6){
String temp = edit.getText().toString();
Log.e("click", temp);
Editor e = this.getPreferences(Context.MODE_PRIVATE).edit();
e.putString("panelID", temp);
e.commit();
startActivityForResult(intent, 0);
} else {
Toast.makeText(this, "Ongeldige invoer (6 cijfer id)", Toast.LENGTH_LONG).show();
}
break;
}
}
What do i do wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
barcodescanner
不会启动两次它返回到创建另一个
barcodescanner
的主活动,尝试删除
onCreate
中的检查并查看结果the
barcodescanner
doesn't start twiceit goes back to main activity which create another
barcodescanner
try to remove the check in
onCreate
and see the result您的问题似乎更多在于您如何管理活动的生命周期。如果您发布 Activity 的 onCreate() 方法的代码,我们也许能够更好地了解您想要完成的任务,并相应地建议解决方案。
Your trouble seems to be more with how you are managing the lifecycle of the activity. If you post your Activity's onCreate() method's code, we might be able to see better what you're trying to accomplish and suggest a solution accordingly.
问题是您不应该每次都在
onCreate()
中启动扫描器。我想象你第一次去扫描仪时,你的父活动被破坏了。当您返回时,必须重新创建它。但这会再次启动扫描仪。然后,当你立即再回去时,这次它还没有被破坏。这与应用程序劫持后退按钮无关。事实并非如此。回来总是有效的。
The problem is that you should not start the scanner every time in
onCreate()
. I imagine that the first time, you go to the scanner, and your parent activity gets destroyed. When you go back, it has to be created again. But this starts the scanner again. And then, when you immediately go back again, it hasn't yet been destroyed this time.It is nothing to do with the app hijacking the back button. It does not. Back always works.
这可能会起作用
在调用 startActivityForResult 后清除编辑文本的数据,如下所示
这应该会使您返回时检查失败
This might work
Clear the data of your edit text after you call startActivityForResult like
This should make your check fail when you go back