android don't lose Fragment when back button is pressed - android-fragments

The user create and add some Fragments to the Activity identified by a tag. I noticed that on pressing the back button the Fragment is destroyed. How can I don't lose all the fragments when the back button is pressing? So that I can navigate without recreating all the time the fragments. I'm asking for a properly way to do this, indeed for now my idea is to override the onBackPressed() and save the Fragment in a List global variable of Fragments before destroy it.

While creating the fragment's instead of FragmentTranscation.replace, use FragmentTranscation.addTobackStack() to add the fragment to backstack and do nothing onBackPressed.
On pressing back button FragmentTranscation will take care of navigating to the previous fragments.

You can try this in the onBackPressed() function.
if (getSupportFragmentManager().getBackStackEntryCount() > 1) {
getSupportFragmentManager().popBackStack();
} else {
super.onBackPressed();
}

Related

How can I clear keyboard focus on a page when I exit and return?

I have a Xamarin.Forms (3.4) app with several Content pages, some of which have several entry boxes. If go to a page, then edit an entry, so far everything is fine. But when I "exit" the page (by doing a Navigation.PopModalAsync() ), and then return to the page, on iOS there is immediately a cursor in the entry, and also the keyboard pops-up. This is not what my user will expect.
On Android, the cursor is in the entry, but it does not pop up the keyboard automatically. This too is not desireable.
For refernce, on UWP it doesn't show focus when returning to the page.
How can I avoid this behavior? Is there a way to have the page "clear" the focus?
You should do something like:
override protected void OnAppearing()
{
entry.Unfocus();
}

JavaFX FXML Controller onClose or equivalent?

Is there a method for a JavaFX controller that gets called when the user leaves that view in the application? The opposite of the initialize() method basically.
I want to do this because I have a page that shows a webcam stream waiting for the user to press a button that takes a snapshot, and I need to close that stream if the user doesn't take a snapshot and leaves the screen. Any other ways of doing this would be appreciated too.
Thanks!
You can use the window's hiding event...
private void detachHook(final Node parent) {
parent.getScene().getWindow()
.addEventFilter(WindowEvent.WINDOW_HIDING, event -> {
//Kill your webcam stream
});
}
You will have to check if you also get the parent as we do in our application, but the window hiding event should work if a node and a scene are present.

Images loaded in ImageView disappear on fragment reLoad

I have a Fragment to edit contents. In this Fragment I have ImageView which displays image picked from gallery or camera This Fragment in turn also opens another fragment. From this fragment when I goes back to Edit fragment displayed image goes disappear and
ImageViews goes blank and every thing else (Edittext,Textviews) remains persistant(filled)
Note that when I goes back to edit fragment its OnCreateView is called not OnCreate method
for example when we replace fragment with another fragment and Pops Back to previous one its OnCreateView method is called first
I can't understand what is happening pls Help.
old, but maybe this can help others.
I solving this by set source path to variable, then call in onResume() method.
#Override
public void onResume() {
super.onResume();
//set again your ImageView source here..
}

Qt: performing an action (scroll) after a widget has been added

I have a horizontal scroll area and dynamically added widgets inside it.
I want it to scroll to the very end whenever a new widget is added, so that the user sees the last widget using this method:
void scrollToEnd()
{
scroll->horizontalScrollBar()->setValue(100000);
}
...
layout->addWidget(widget);
scrollToEnd();
However, there's a delay between calling layout->addWidget() and actual widget appearing. So calling scrollToEnd() does nothing.
If I make apause by, for example, showing a MessageBox, everything works fine.
Is there a way to wait till the widget is displayed, and then scroll the scroll area?
addWidget probably schedules the actual addition of the widget to after the reentering of the event loop, so you should do the same and call the scrollToEnd method asynchronously (it has to be a slot):
layout->addWidget(widget);
QTimer::singleShot(0, this, SLOT(scrollToEnd()));

Who should remove popup, calling app or popup itself?

Here is the scenario...I have an application that has an "Open" button. When you click the open button, a TitleWindow pops up (via the PopUpManager) a dialog that allows you to select a record. In that window, you can either click "Ok" once you've selected a record, or cancel the dialog which should close the window.
What is the best way to handle this, knowing I need to return the record number to the main application? Right now I have the main application opening up the custom TitleWindow. In the TitleWindow, I have an event that fires when the user clicks "Ok" and a record is selected. That's how the main application listens for the recordId. What I'm wondering is if I can do something like this:
private function RecordSelected():void
{
this.selectedRecord = someControl.selectedIndex;
this.dispatchEvent(new Event("RecordSelected"));
PopUpManager.removePopup(this);
}
Will the instance of the TitleWindow stay active until after the event is handled by the main application, or could it be removed before the main application fully handles the event (thus ending with a null reference exception)?
Alternatively, is it better to close the window in the event handler like so:
private function titleWindow_RecordSelected(event:Event)
{
var openDialog:CustomTitleWindow = CustomTitleWindow(e.currentTarget);
this._selectedRecord = openDialog.selectedRecord;
PopUpManager.removePopup(openDialog);
}
I'm also wondering if sending the selection out in an event is the best way to do this also. Any insight would be appreciated. Thanks in advance.
What is the best way to handle this,
knowing I need to return the record
number to the main application?
Best is always subjective, but I usually handle closing of the window in the window component; and then dispatch an event with appropriate data (In this case Record No) back to the application. It sounds like your 90% there. Just instead of dispatching an event Event, create a custom Event class with your recordID. Then it won't matter if the pop up is still in memory or not when you need to use the record ID.

Resources