android - 阻止 Activity 启动旧实例
问题描述
我正在尝试显示要保存在 FirebaseDatabase 中的用户组中的联系人。
该应用程序第一次运行良好,但是当我再次执行路径时,按下按钮后,该应用程序不会重复第一个结果。
下面是一个例子: MainActivity 调用 GroupActivity;GroupActivity 调用 NewUserActivity,NewUserActivity 在按下按钮后返回 MainActivity。
在 NewUserActivity 中有与 FirebaseDatabase 相关的代码,其中联系人被加载并添加到不同的路径中。
这是 Logcat 和总结的解释:第一轮。该应用程序正常启动在第一个屏幕中显示组的名称
debinf mainpage: onStart
debinf mainpage: onResume
debinf mainpage: onPause
debinf mainpage: onResume
这是第一次单击 RecyclerView 中的项目。这里显示了组中的用户。在屏幕底部有一个 floatActionButton
debinf mainpage: onPause
debinf groupactivity: onStart
debinf groupactivity: onResume
debinf mainpage: onStop
在这里,我单击了 floatActionButton 以转到 NewUserActivity。在这里,我将组中的用户复制到 FirebaseDatabase 中的临时路径,并将我的联系人列表加载到我的设备中的 RecyclerView 中。
debinf groupactivity: onPause
debinf newuser: onCreate
debinf groupact: passed intentextrasA
debinf groupact: passed intentextrasB
debinf newuser: onStart
debinf newuser: onResume
debinf newuser: Load peopleA to ContactAdded
debinf newuser: Load peopleB to ContactAdded
debinf newuser: Load peopleC to ContactAdded
debinf groupactivity: onStop
debinf groupactivity: onDestroy
这里显示了上部 RecyclerView 中加载的联系人和下部 RecyclerView 中我设备的联系人列表。为简化示例,此处未显示对临时路径添加和删除联系人的操作。在选择了组内和组外的人员后,我按下按钮以完成并将联系人保存在 FirebaseDatabase 中的组路径中。
debinf newuser: button pressed
debinf newuser: Added peopleA inside button
debinf newuser: Added peopleB inside button
debinf newuser: Finish inside the button
debinf newuser: onPause
debinf mainpage: onRestart
debinf mainpage: onStart
debinf mainpage: onResume
debinf newuser: onStop
debinf newuser: onDestroy
正如预期的那样,在我按下 NewUserActivity 中的按钮后调用 MainActivity。到现在为止还挺好
第二轮。现在让我们再次执行相同的过程。我点击组名进入 GroupActivity。
debinf mainpage: onPause
debinf groupactivity: onStart
debinf groupactivity: onResume
debinf mainpage: onStop
现在我点击了floatActionButton。
debinf groupactivity: onPause
debinf newuser: onCreate
debinf groupact: passed by intentextrasA
debinf groupact: passed by intentextrasB
debinf newuser: onStart
debinf newuser: onResume
debinf newuser: Load peopleA to ContactAdded
debinf newuser: Load peopleB to ContactAdded
debinf newuser: button pressed
debinf newuser: Added peopleA inside button
debinf newuser: Added peopleB inside button
debinf newuser: Finish inside the button
debinf groupactivity: onStop
debinf groupactivity: onDestroy
可以看出,按钮是自己按下的。
我希望能够让人们走上新的道路,然后再次选择谁进谁出。
我认为这与保存的实例有关。
我该怎么做才能防止 NewUserActivity 自行运行?因为这会导致添加和删除联系人的过程混乱。除此之外,我最终从组中删除了我的所有联系人。
提前致谢!
更多信息:MainActivity 适配器的单击侦听器
holder.mLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent groupActivity = new Intent(mContext,GroupActivity.class);
groupActivity.putExtra("groupName",groupList.get(position).getGroupId());
groupActivity.putExtra("groupPushKey",groupList.get(position).getGroupKey());
// Because we are in Adapter, the Adapter does not know what context is
// so the activity is started from the mContext
mContext.startActivity(groupActivity);
}
});
GroupActivity 中的 FloatActionButton
AddContact = (FloatingActionButton) findViewById(R.id.addContactInGroup);
AddContact.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent newUserToGroup = new Intent(getApplicationContext(),NewUserToGroupActivity.class);
newUserToGroup.putExtra("groupName",groupNameIntent);
newUserToGroup.putExtra("groupPushKey",groupKeyIntent);
startActivity(newUserToGroup);
finish();
}
});
NewUserActivity 中按钮的单击侦听器
mUpdateGroupBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
updateGroupUsers();
}
});
在哪里:
私人无效更新组用户(){
RootRef.child("ContactAdded").child(currentUser).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
Log.i("debinf newuser", "button pressed");
Map<String, Object> updateGroupUsersMap = new HashMap<>();
for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) {
Log.i("debinf newuser", "Added people inside button : " + childSnapshot.getKey());
updateGroupUsersMap.put(childSnapshot.getKey(),"");
}
FirebaseDatabase.getInstance().getReference().child("Group").child(currentUser).child(groupKeyIntent).child(groupNameIntent).setValue(updateGroupUsersMap);
FirebaseDatabase.getInstance().getReference().child("ContactAdded").child(currentUser).removeValue();
Log.i("debinf newuser","Finish inside the button");
finish();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
解决方案
你做的事情有问题updateGroupUsers
。
这在用户按下按钮时调用,因此您需要立即执行某些任务,而不是注册 aValueEventListener
并等待onDataChange
发生,然后才执行此操作。
显然,由于您从未删除添加的侦听器,因此侦听器仍在侦听数据更改,并且每当检测到新的数据更改时,它会再次执行操作,错误地将“按下按钮”打印到日志中,即使按钮没有按下。
我建议要么自己清理,一旦你不需要监听器,就删除它。或者最好尝试执行您打算执行的操作而不将其放入某个侦听器中,因为这可能会在您当时期望的数据更改和您想要执行的操作之间引入一些竞争条件。
推荐阅读
- javascript - Change style of single elements in Primefaces datatable with javascript?
- python - How can I format this JSON String into a JSON object?
- math - Grouping 1-12 in groups of 4 with the least number of duplicates
- database - PgBouncer - Can users in "admin_users" use "auth_query" to connect instead of userlist.txt?
- swift - Ordering of events from addGlobalMonitorForEvents
- javascript - How to make amcharts animate plugin work with RequireJS?
- javascript - IE11 and JavaScript array.map()
- python - Converting a YAML file to Python JSON object
- c++ - 实时更新控制台中的字符串内容
- excel - 遍历所有使用某个数据透视缓存的数据透视表