阻止活动启动旧实例

时间:2019-01-12 20:08:54

标签: android android-studio

我正在尝试显示要保存在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

按预期,MainActivity被称为 在按下NewUserActivity中的按钮之后。 到目前为止一切顺利

第二轮。 现在,让我们再次执行相同的过程。 我单击了组名称以转到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();
    }
});

位置:

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

    }
});

}

1 个答案:

答案 0 :(得分:1)

您在updateGroupUsers中所做的事情有误。

这是在用户按下按钮时调用的,因此您需要立即执行一些任务,而不是注册ValueEventListener并等待onDataChange发生,然后才执行此操作。

很显然,由于您从未删除添加的侦听器,所以侦听器仍在侦听数据更改,并且每当检测到新数据更改时,侦听器都会再次执行操作,即使该按钮错误地将“按钮按下”打印到日志中没有被按下。

我建议您自己清理一次,一旦不需要侦听器,请将其删除。 或者最好尝试执行您打算执行的动作而不将其放入某个侦听器中,因为这可能会在您当时期望的数据更改与您想要执行的动作之间引入某种竞争条件。