Home > Mobile >  Android entry-level problem (nested fragments, callback interface), sincerity invite great god reply
Android entry-level problem (nested fragments, callback interface), sincerity invite great god reply

Time:09-27

Beginners, encounter unexplained problem, hope for a great god help! Thank you very much!
Problem description:
An activity (hosts), three fragments (A, A1, A2), the relationship is: A is sub fragments embedded in the host in the activity, A son and two fragments, this two child fragments in A nested (do not use the viewpager), in A, use getChildFragmentManager loading A1 and A2 () method, and use the show/hide way of A1, A2 switching, code is as follows:
If (targetFragment!=null) {
//open the relevant fragments
FragmentManager FragmentManager=getChildFragmentManager ();//create FragmentManager object FragmentTransaction transaction=FragmentManager. BeginTransaction ();//create FragmentTransaction object
if (! TargetFragment. IsAdded ()) {//if you want to display targetFragment didn't add
The transaction
. Hide (mCurFragment)//hide the current Fragment
. The add (R.i d.s shpfrag targetFragment, targetFragment. GetClass (). The getName ())//add targetFragment
Commit ();
} else {//if you want to display targetFragment have add
The transaction//hide the current Fragment
. Hide (mCurFragment)
. The show (targetFragment)//show targetFragment
Commit ();
}
//update the current Fragment for targetFragment
MCurFragment=targetFragment;
}
//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Right now, I am A onActivityCreated (Bundle savedInstanceState) set A timer, the code is as follows:
The timer=new timer ();
The timer. The schedule (new TimerTask () {
@ Override
Public void the run () {
Message msg=new Message();
MSG. Arg1=3;
handler.sendMessage(msg);//sendMessage () method is used to press the Message into the Message stack,
}
}, 0100, 0);
Then, in A set up an interface so that the callback, the code is as follows:
Public interface Apagecallback {
Void interval (int flag);
}
Finally, in the A1 and A2, interval interface is implemented, is to monitor the timer,
The code is as follows:

A1 interface implementations@ Override
Public void interval (int flag) {//timer state
The d (" A1flag * * * * * * * * * * * * * ", String. The valueOf (flag);
}
A2 interface implementation of
@ Override
Public void interval (int flag) {//timer state
The d (" A2flag * * * * * * * * * * * * * ", String. The valueOf (flag);
}
//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Is two child fragments together to achieve the father fragment interface
//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Run up and not an error, but the problem is!!!!
Open at the beginning of A A1, then you can see the d (" A1flag * * * * * * * * * * * * * ", String. The valueOf (flag)); Every two seconds to print out a log, and then, I again click the button to open the A2 (A1 is hidden, A2show A1hidde) then you can see the d (" A2flag * * * * * * * * * * * * * ", a String, the valueOf (flag)); But A1 listening in?????? Even if I again open see A1 (A1show A2hide) or "A2flag * * * * * * * * * * * * *" A1 is hidden, not destroyed, why don't you get the A1 interface callback??? , I also enter the d printing over A, A1, A2 lifecycle, only load has A life cycle, the show/hide don't walk any life cycle, then why A1 couldn't back? I hope A1, A2 can listen to the callback, at the same time how to do? Hope the great god help! Thank you very much!

CodePudding user response:

Problem complement:
I am a beginner, there are many problems not understand, may also have this problem won't be a problem, of course, I also think of any other solution, but it not to be able to rub along the mouth! Why it is hidden, not destroyed, but can only print A2? If as long as the child hidden fragments were unable to callback, but explain impassability, because from A2 switch back to the A1 (A2hide) at this time also can print out the log, again, in other words, if the fragments are hidden state to do anything, then why should use the show/hide mode switch? Rather than replace more happy! In addition, if the correction are not possible, the use of local radio, of course, also won't be able to receive?

CodePudding user response:

Your callback to monitor is a variable Apagecallback or List , if only a single variable, when you set A2 to listen, A1 can be replaced, if you want to response to monitor, the need to use List The two are stored

CodePudding user response:

Upstairs brother tip very to! Thank you very much! Thank you very much! But my level is limited, heads a day! What also didn't get out! Really sad! Brother can involvement to admire the next example? Thank you very much!

CodePudding user response:

In your FragmentA Apagecallback listening to Set And then put the setCallback method is modified to addCallback, the code is to add the callback is added to the Set collection, traverse the Set where the callback to invoke the callback method

FragmentA code roughly the following
 
Set CallbackSet=new HashSet<> (a);

Public void addCallback (Apagecallback callback) {
CallbackSet. Add (the callback);
}

//assume that this method is the way you callback
Public void call (int flag) {
//traverse the set
For (Apagecallback callback: callbackSet) {
The callback. The interval (flag);
}
}
  • Related