gpt4 book ai didi

android - 一旦在 Firebase 中更改了子值,是否有任何方法可以防止 onCreate 调用?

转载 作者:行者123 更新时间:2023-11-30 04:58:33 24 4
gpt4 key购买 nike

我想在 Firebase 中更改数据时更改 TextView 中的文本。然而,我想要的结果只是TextView中的变化,结果却是再次调用了onCreate方法。那么,当数据发生变化时,如何防止方法调用呢?

我尝试在新的线程和处理程序中运行它,但结果还是一样。

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
Log.e("onCreate","oncreate");

DatabaseReference statusRDB = FirebaseDatabase.getInstance().getReference().child("User").child(friendID).child("CurrentStatus");
statusRDB.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String status = dataSnapshot.child("Status").getValue().toString();
String lastActive = dataSnapshot.child("LastActive").getValue().toString();
mStatusText.setText(showActive.showActiveTime(status,lastActive));

}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

}
});

}

这是child更改后的日志。如我们所见, Activity 已暂停,然后将再次创建。


V/FA: Recording user engagement, ms: 11975
V/FA: Connecting to remote service
V/FA: Activity paused, time: 54071124
D/FA: Logging event (FE): user_engagement(_e), Bundle[{ga_event_origin(_o)=auto, engagement_time_msec(_et)=11975, ga_screen_class(_sc)=ChatActivity, ga_screen_id(_si)=4408365996041177143}]
V/FA: onActivityCreated
V/FA: Connection attempt already in progress
D/TextView: setTypeface with style : 0
D/TextView: setTypeface with style : 0
setTypeface with style : 0
setTypeface with style : 0
setTypeface with style : 0
V/FA: Activity resumed, time: 54071174
D/SecWifiDisplayUtil: Metadata value : none
D/ViewRootImpl: #1 mView = com.android.internal.policy.PhoneWindow$DecorView{8a14704 I.E...... R.....ID 0,0-0,0}
D/FA: Logging event (FE): screen_view(_vs), Bundle[{ga_event_origin(_o)=auto, ga_previous_class(_pc)=ChatActivity, ga_previous_id(_pi)=4408365996041177143, ga_screen_class(_sc)=ChatActivity, ga_screen_id(_si)=4408365996041177144}]
V/FA: Connection attempt already in progress
Connection attempt already in progress
D/TextView: setTypeface with style : 0
setTypeface with style : 0
setTypeface with style : 0
setTypeface with style : 0
setTypeface with style : 0
setTypeface with style : 0
setTypeface with style : 0
D/TextView: setTypeface with style : 0
setTypeface with style : 0
setTypeface with style : 0
setTypeface with style : 0
setTypeface with style : 0
setTypeface with style : 0
D/TextView: setTypeface with style : 0
setTypeface with style : 0
setTypeface with style : 0
setTypeface with style : 0
setTypeface with style : 0
setTypeface with style : 0
setTypeface with style : 0
D/TextView: setTypeface with style : 0
setTypeface with style : 0
setTypeface with style : 0
setTypeface with style : 0
setTypeface with style : 0
setTypeface with style : 0
D/TextView: setTypeface with style : 0
setTypeface with style : 0
setTypeface with style : 0
setTypeface with style : 0
setTypeface with style : 0
setTypeface with style : 0
setTypeface with style : 0
D/TextView: setTypeface with style : 0
setTypeface with style : 0
setTypeface with style : 0
setTypeface with style : 0
setTypeface with style : 0
setTypeface with style : 0
setTypeface with style : 0
D/TextView: setTypeface with style : 0
setTypeface with style : 0
setTypeface with style : 0
setTypeface with style : 0
setTypeface with style : 0
setTypeface with style : 0
setTypeface with style : 0
setTypeface with style : 0
D/TextView: setTypeface with style : 0
setTypeface with style : 0
setTypeface with style : 0
setTypeface with style : 0
setTypeface with style : 0
setTypeface with style : 0
setTypeface with style : 0
D/TextView: setTypeface with style : 0
W/DisplayListCanvas: DisplayListCanvas is started on unbinded RenderNode (without mOwningView)
D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 96 - 0, 0) vi=Rect(0, 96 - 0, 0) or=1
D/FA: Connected to remote service
V/FA: Processing queued up service tasks: 4
I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@204ceb1 time:31322606
D/ViewRootImpl: #3 mView = null
V/FA: Inactivity, disconnecting from the service
V/FA: Recording user engagement, ms: 18053
Connecting to remote service
V/FA: Activity paused, time: 54089226
D/FA: Logging event (FE): user_engagement(_e), Bundle[{ga_event_origin(_o)=auto, engagement_time_msec(_et)=18053, ga_screen_class(_sc)=ChatActivity, ga_screen_id(_si)=4408365996041177144}]
V/FA: Connection attempt already in progress
V/FA: Activity resumed, time: 54089635
D/FA: Logging event (FE): screen_view(_vs), Bundle[{ga_event_origin(_o)=auto, ga_previous_class(_pc)=ChatActivity, ga_previous_id(_pi)=4408365996041177144, ga_screen_class(_sc)=FriendProfile, ga_screen_id(_si)=4408365996041177142}]
D/ViewRootImpl: #3 mView = null
V/FA: Connection attempt already in progress
Connection attempt already in progress
V/FA: Screen exposed for less than 1000 ms. Event not sent. time: 39
Connection attempt already in progress
Activity paused, time: 54089644
V/ActivityThread: updateVisibility : ActivityRecord{5820612 token=android.os.BinderProxy@4d012e7 {com.example.user.convo/com.example.user.convo.profile.FriendProfile}} show : true
D/FA: Connected to remote service
V/FA: Processing queued up service tasks: 5
I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@4d012e7 time:31340945
E/ViewRootImpl: sendUserActionEvent() mView == null
D/FA: Application going to the background
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
(HTTPLog)-Static: isSBSettingEnabled false
D/FA: Logging event (FE): app_background(_ab), Bundle[{ga_event_origin(_o)=auto}]
I/art: Background sticky concurrent mark sweep GC freed 87634(4MB) AllocSpace objects, 28(748KB) LOS objects, 11% free, 39MB/44MB, paused 1.322ms total 114.785ms
V/FA: Inactivity, disconnecting from the service

预期的结果是只会更改 Text View 中的文本,而不是调用整个 onCreate 并开始新的 Activity。

最佳答案

上面的sudo代码不会再调用oncreate方法了。在上面的代码中,您使用的是 addListenerForSingleValueEvent,它只会被调用一次。

关于android - 一旦在 Firebase 中更改了子值,是否有任何方法可以防止 onCreate 调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58704569/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com