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

我想在Firebase中更改数据时更改“文本视图”中的文本。但是,我想要的结果只是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) {

            }
        });

}

这是更改子级后的日志。如我们所见,活动被暂停,然后将再次创建。


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),ga_previous_class(_pc)=Chatactivity,ga_previous_id(_pi)=4408365996041177143,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,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),engagement_time_msec(_et)=18053,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),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

预期结果是仅将更改“文本视图”中的文本,而不是调用整个onCreate并启动新活动。

wuzh166 回答:一旦在Firebase中更改了子值,是否有任何方法可以阻止onCreate调用?

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

本文链接:https://www.f2er.com/3162266.html

大家都在问