How to go back two times in freagments - android-fragments

I have three fragments f1,f2,f3.
f1 contain a list click select a item from list then fragment f2 loads it again contain another list. select a item from list fragment f3 gets loaded.
this fragment f3 contain 3 button.
Button 1 again open a new fragment.
button 2 for go back to f2 fragment.
button 3 for go back to f1 fragment.
I want to know how to go back from f3 to f1 if i click on button 3 in fragment f3???

I think you need a good sample or tutorial. So start with this webpage # Google Fragments. Search for "here's how you can replace one fragment with another...". Code snippet from the webpage:
// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
Notice the replace method. Afterwards post code and layouts.
Good luck and have fun...

Related

RecyclerView does not maintain scroll position on Room database update

I have two fragments, the first with a recyclerView to show all notes from a Room database and a second fragment where notes can be added, edited or deleted in the database. Navigation between the fragments is by JetPack Navigation. My problem is that after editing and saving a note in Room, when I navigate back to the List fragment, the recyclerView does not stay on the edited note. It defaults to the first item in the list. I am not sure how to inform the List fragment if a note was edited or added, and where it would be in the updated list.
In the Update Fragment, I add or edit note, and navigate back to List Fragment.
binding.btnSave.setOnClickListener {
val note = binding.etNote.text.trim().toString()
if (!args.isEditing){
//save a new note
val newNote = Note(0,note,date)
mNoteViewModel.addNote(newNote)
}else{
//save edited note
val updatedNote = Note(noteId,note,date)
mNoteViewModel.updateNote(updatedNote)
}
val action = UpdateDataDirections.actionUpdateDataToNoteList()
findNavController().navigate(action)
In the recycler adapter I have a function to update the note list.
var allNotes = listOf<Note>()
fun setData(notes:List<Note>){
this.allNotes = notes
notifyDataSetChanged()
}
In the List Fragment I have an observer to monitor changes to the database and update the recyclerView. The notes are sorted by date in my Room query. For now, I only need the recycler to scroll to the proper position when a note is edited, but it would also be useful if it could scroll to the position of a newly added note.
val adapter = NoteAdapter(viewClickListener)
val layoutManager = LinearLayoutManager(context)
binding.recycler.layoutManager = layoutManager
binding.recycler.setHasFixedSize(true)
binding.recycler.addItemDecoration(DividerItemDecoration(context,LinearLayoutManager.VERTICAL))
binding.recycler.adapter = adapter
mNoteViewModel.getAllNotes().observe(viewLifecycleOwner, {
it.let{
adapter.setData(it)
}
})

JavaFX block MouseEvent until delayedExecutor finishes its job

I'm trying to create a simple memory match game using Java 11 and JavaFX.
I have a scenario where two cards don't match. Before the non-matching cards are flipped back I'd like to have 800 milliseconds delay, so the user can see what was the second selected card. I'm using this code and it works fine:
CompletableFuture.delayedExecutor(800, TimeUnit.MILLISECONDS).execute(() -> {
firstRevealedCard.setFaceDown(); // sets an ImageView's setImage() method
secondRevealedCard.setFaceDown(); // sets an ImageView's setImage() method
firstRevealedCard = null; // sets instance variable to null
secondRevealedCcard = null; // sets instance variable to null
});
All the cards in the game are in a 4 column and 2 row GridPane.
Each cell in the grid is an ImageView set to an Image.
Each card object has a Mouse (click) event attached to it:
// Add all cards to the deck
for ( int card_ID = 1; card_ID < unique_cards+1; card_ID++ ) {
Card card1 = new Card( card_ID );
card1.setOnMouseClicked( (MouseEvent event) -> { onMouseClicked(card1); });
addCard(card1);
Card card2 = new Card( card_ID );
card2.setOnMouseClicked( (MouseEvent event) -> { onMouseClicked(card2); });
addCard( card2 );
In the code above card1 and card2 are two cards having the same picture. They're matching cards.
The cards are stored in an ArrayList. After all the cards has been added to the ArrayList, the ArrayList is shuffled.
The ArrayList store Card objects and each object has its own mouse event listener attached.
THE ISSUE: When two cards don't match, the non-matching cards need to flip back and the 800 milliseconds timeout works well using the CompletableFuture.delayedExecutor, but I want the "mouse event" to be blocked or locked until the Executor fininshes, otherwise, let's say the user clicks an other card, but the non-matching cards didn't have the chance to flip back.
First card selected - Apple
Second card selected - Banana
Two non-matching cards
Executor starts
100 milliseconds
100 milliseconds
100 milliseconds
100 milliseconds
the user clicks another card (throws an exception)
just because user clicked another card before executor finishes
100 millisecond
...
...
at 800 milliseconds
Executor finishes, cards are flipped back
Exception in thread "ForkJoinPool.commonPool-worker-3" java.lang.NullPointerException
at memorygamefx.CardDeck.lambda$onMouseClicked$0(CardDeck.java:85)
at java.base/java.util.concurrent.ForkJoinTask$RunnableExecuteAction.exec(ForkJoinTask.java:1426)
at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:290)
at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1020)
at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1656)
at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1594)
at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:177)
I would be grateful for any suggestion. Thank you!
Don't use delayedExecutor for this. If you continue to do so, wrap the delayed code you invoke in Platform.runLater, so that it runs on the JavaFX thread otherwise weird stuff might happen.
Instead use a PauseTransition such as this:
PauseTransiton pause = new PauseTransition(Duration.millis(800));
Node disabledNode = scene.getRoot();
disabledNode.setDisable(true);
pause.setOnFinished(e -> {
disabledNode.setDisable(false);
// delayed code that you would like to run.
});
pause.play();
The example disables the entire scene when the pause is executing.
A binding can also be used instead, but manually setting and unsetting the disable property is probably better:
scene.getRoot().disableProperty().bind(
Bindings.equal(pause.statusProperty(), Animation.Status.RUNNING)
);
Disabling the scene will, by default, grey it out to give feedback that it is disabled. If you don't want that, you could instead add or remove a mouse event filter (please read attached link to understand this if you don't know what it is) on a scene or a hierarchy or nodes to prevent input that you don't want the app to process during the pause.
Another thing you could do to disable mouse input, is to set the root node to mouseTransparent while the pause is running, that way mouse input will be ignored. That would be similar to defining your own filters to ignore mouse events, but much easier to implement.
Some advantage of a PauseTransition are:
Everything runs on the JavaFX thread, so you don't need to deal with multi-threading issues.
It has a rich API so it is quite flexible.
It is reusable.

How to handle buttonclicked event of DDDW?

Situation:
I have a button in my DDDW and i want to capture buttonclicked event.
Problem:
when i click on the button in DDDW the buttonclicked event of DW Control is not fired and ItemChanged event is fired for DW control.
Question:
How do i capture buttonclicked event for the button in DDDW? Or is there any other way i can delete a row from DDDW when delete button is clicked for a particular row?
PowerBuilder 12.5
According to the PB help, a DataWindowChild has no events :|
But, that doesn't mean we still can't hook into it via the DW control's itemchanged event. Note: this is a hack, and underwent some very-limited testing. But, it demonstrates a point, I guess.
Here's what I did:
Created a DataWindow with the code and name columns, and a computed field (for the red X) named delete_button
Created another DataWindow and painted that DW as a DDDW on there, named profession
In my window control's open event, I got the DDDW from the DW and stuck it in an instance variable:
dw_1.GetChild("profession", REF idwc_profession)
Then, coded the itemchanged event for the DW control:
// dw_1::itemchanged
//
// - DDDW is named "profession"
IF dwo.Name = "profession" THEN
IF IsValid(idwc_profession) THEN
string ls_clickedobject
// Get the DataWindowCHILD object where the pointer was clicked:
ls_clickedobject = idwc_profession.GetObjectAtPointer()
IF IsNull(ls_clickedObject) OR (ls_clickedobject = "") THEN RETURN
// Return from GetChild is <column name>~t<row number>; let's get
// the position of the tab character so we can parse it
long ll_tabPos
ll_tabPos = Pos(ls_clickedObject, "~t")
IF ll_tabPos > 0 THEN
string ls_clickedDddwColumn
ls_clickedDddwColumn = Trim(Left(ls_clickedObject, ll_tabPos - 1))
// Check to see if we've clicked on the computed field with the delete button
IF Lower(ls_clickedDddwColumn) = "delete_button" THEN
long ll_clickedDddwRow
// grab the row we want to delete
ll_clickedDddwRow = Long(Trim(Right(ls_clickedObject, Len(ls_clickedObject) - ll_tabPos)))
IF ll_clickedDddwRow > 0 THEN
// delete the row from the DDDW
idwc_profession.DeleteRow(ll_clickedDddwRow)
SetNull(data) // reset our data
END IF
END IF
END IF
END IF
END IF
RETURN
Also note that you may have to play around with the return value from itemchanged to get it to do what you want. And, if you want to automatically dropdown the DDDW again after the deletion happens, you'd might be able to use the Send() method to do so (I don't know the right "number" for that, though).

how to make an object visible again in game maker

Fist of all, I'm really sorry for my bad English and I pretty new to game maker .
I have 2 object in the game : obj_apple and obj_door( I unchecked visible box)
my question is
how can I make an obj_door visible in the room when all the obj_apple are destroyed?
Object obj_door, Step event, place code (Add event -> Step -> Step -> tab Control -> section Code, first icon (Execute code)):
if !instance_exists(obj_apple) visible = true;
Another option, so you aren't making a check in every step event, is to put the check for the number of obj_apple in the destroy event of obj_apple.
For example, in the destroy event of obj_apple you would have:
if (instance_number(object_index) == 0) {
with (obj_door) {
visible = true;
}
}

how to replace a fragment with another fragment in android 4.0.4

I try to replace a fragment with another fragment by pressing a button.
This is Successfull on android 4.1.2,but is failed on android 4.0.4
The code is:
if(f13!=null)
{
transaction.remove(f13);
transaction.replace(R.id.place,f10,"f10");
transaction.commit();
}
where f13 and f10 are fragments.
I added this statment:
transaction.addBackstack(null);
transaction.replace(R.id.place,f10,"f10");
transaction.commit();
The app is running,but when i click back button in my mobile to return f13, the fragment f10 show under f13.
I want when i am clicking back button the app close.
Let me be clear, you want to manage your backstack so that when you hit the Back button, your app should close. For that issue, use popBackStack method of FragmentManager class. Code example using your pseudo code:
Fragment newFragment = new ExampleFragment(); // "f10" fragment
FragmentManager manager = getActivity().getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.place,f10, newFragment);
transaction.commit();
manager.popBackStack();
If you want to remove all items in the stack:
manager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
I never tried POP_BACK_STACK_INCLUSIVE. Pls keep us posted. Good luck, Tommy Kwee

Resources