Android Studio 防止活动启动旧示例

oipij1gg  于 2022-11-16  发布在  Android
关注(0)|答案(1)|浏览(211)

我正在尝试显示要保存在FirebaseDatabase的用户组中的联系人。
应用程序第一次运行正常,但当我再次按下按钮后,应用程序不会重复第一次的结果。
以下是一个示例:
主活动调用组活动;
组活动调用新用户活动并
按下按钮后,NewUserActivity返回到MainActivity。
在NewUserActivity中,存在与FirebaseDatabase相关的代码,在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);

    }
});

组活动中的浮动操作按钮

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();
    }
});

其中:

private void 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) {

        }
    });

}
eivgtgni

eivgtgni1#

你在updateGroupUsers里做的事情有问题。
当用户按下一个按钮时调用此函数,因此您需要立即执行某个任务,而不是注册一个ValueEventListener并等待onDataChange发生,然后才执行此操作。
显然,由于您从未删除添加的侦听器,因此侦听器仍在侦听数据更改,并且每当检测到新的数据更改时,它都会再次执行该操作,即使按钮没有被按下,也会在日志中错误地打印“button pressed”。
我建议您在自己之后进行清理,一旦不需要侦听器,就将其删除。或者,最好尝试执行您打算执行的操作,而不将其放在某个侦听器中,因为这可能会在您当时预期的数据更改和您想要执行的操作之间引入一些争用条件。

相关问题