I know this is very basic, but I am not able to figure out why I am getting this.
I am running a splash screen, and in the splash screen I am running a background thread for performing my required operation to contact server.
After the background thread finishes its task, the listener in the splash screen creates an object for the next screen and sends it to the method below:
public void swapScreen(final TopNewsScreen _tn)
{
UiApplication.getUiApplication().invokeLater(new Runnable(){
public void run()
{
UiApplication.getUiApplication().popScreen();
UiApplication.getUiApplication().pushScreen(_tn);
}
});
}
Help of any sort is welcome.
The code you posted looks fine. I've seen "push modalscreen called from noneventhread" happen when there is a Dialog.inform or Dialog.ask call somewhere on a background thread.
Double check your background thread, and make sure it doesn't try to throw up some UI.
popScreen() takes a screen as an argument, so there should be one there.
When the last (or only) screen your application has pushed to the display is removed, the application exits. I would suggest pushing _tn first, then poping the splash screen.
The better way to handle this situation is:
class SplashScreen extends FullScreen
{
protected void onObscured()
{
close();
}
}
And simply push your screen (TopNewScreen) like you are doing. When the SplashScreen is not shown anymore, the screen closes by itself.
Related
I am new to mobile development. I would like to show a different screen if GPS is not enabled. I have put the code in the view did appear to show the new screen this works most of the time. However when app returns from background the new screen is not shown. After debugging i found that when the app returns to foreground Viewdidload/viewdidappear/the constructor of the controller is not called.
Is there an override which I can use to when the app returns from background on the controller. Also after research I found this link
My Research
If this is the way forward, can someone help me convert this code to Xamarin ios.
Thanks in advance.
In Xamarin IOS , adding Notifications in ViewDidLoad Method , can do that in ViewDidAppear.
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
UIApplication.Notifications.ObserveWillEnterForeground ((sender, args) => {
Console.WriteLine("Welcome back!");
//Add code from ViewDidAppear method here
});
}
Here is the IOS LifeCycle document.
You can also use default notification center and then call your ViewModel methods.
NSNotificationCenter.DefaultCenter.AddObserver(UIScene.WillEnterForegroundNotification,
notification =>
{
ViewModel.ViewAppearing();
});
Bonus:
If you'll put this code in ViewDidLoad then it will add multiple observers which will cause the observer to trigger multiple times.
Save NSNotificationCenter.DefaultCenter.AddObserver return token and then in ViewWillDisappear you can just dispose the token. It will now not called multiple times now.
I want to block my screen for listening a tap or click when the show.status("Please Wait",2 AnyTimeLimit); is invoked.
Actually I'm sending a hit to a web service and meanwhile I'm displaying a message "Please Wait" with 2 seconds wait. But after those 2 seconds It starts listening the tap/click. How can I block them to listen tapEvent/ClickEvent when displaying message?
Thanks,
You will have to make a custom screen that overrides PopupScreen for that. In your implementation, you will have to override onUiEngineAttached, like so -
protected void onUiEngineAttached(boolean attached)
{
super.onUiEngineAttached(attached);
if(attached)
{
// start server hit thread, which closes the screen when done
}
}
After that, just push the screen using UiApplication.getUiApplication.pushModalScreen(). You will want to keep the data you received in a byte array somewhere in the screen's instance which you can retrieve by calling a getter once the screen has been popped.
We now that in monotouch and for iPhone / ipad application when we want to have splash screen before app lunch we should to set launch image in info.plist file and it will show this image before application launches.
But what is the best way to implement a splash screen when we want to have a splash that runs some heavy codes in background and not disappear until these operations had not completed? Some codes like downloading application config from internet and saving theme that often used in splash screen.
Possible solution:
Make a SplashViewController, which contains same image as app's splash image. Also it contains UIActivityIndicatorView;
In AppDelegate's FinishedLaunching method make new instance of SplashViewController, set it as window.RootViewController, call:
activityIndicator.StartAnimating();
Runs some heavy codes in background;
When it's done, set window.RootViewController to ViewController, which is app's starting point.
BTW, there is another solution: create main UIViewController, set it as Window.RootViewController immediately in AppDelegate's FinishedLaunching method. Then create and show modally splashViewController by this code:
...
MainViewController.PresentModalViewController(splashViewController, true);
...
Hiding modal UIViewController is possible via calling code:
DismissModalViewControllerAnimated(true);
Note that since iOS 6 PresentModalViewController becomes deprecated method. So, for many iOS versions compatibility you could code special method for showing modal UIViewController.
public void ShowModalViewController (UIViewController vc, bool animated)
{
if (UIDevice.CurrentDevice.CheckSystemVersion(6, 0)) {
MainViewController.PresentViewController(vc, animated, null);
} else {
MainViewController.PresentModalViewController(vc, animated);
}
}
In my app I fave some screens one after another, and I need to pop to home screen any time. Is there any way to find out is active screen is the first one? Or, may be, there is any function to pop to root screen without cycle of poping to previous ones?
EDIT In different words, I need my app to go to previous screen on click on blackberry "back" button (it do this without any additional code), and go to the screen, user first see when starts this application on click on "Home" button from my user interface
This helps you any time and any where:
Write this method in startup class(StartUp.java):
public static void popupScreens()
{
int screenCount = UiApplication.getUiApplication().getScreenCount();//Gives how many screens are active state in background;
for (int i = 0; i < screenCount; i++)
{
Screen screen = UiApplication.getUiApplication().getActiveScreen();
UiApplication.getUiApplication().popScreen(screen);
}
}
and call this method at any place with class name(EX: StartUp.popupScreens) in any where then it popup all the screens in the stack.
First, you will need to organize your screens, application logic and clearly define what screens you want to be on the UI stack and those that don't. For those screens that don't need to be on UI stack, you can dismiss them automatically when another screen is pushed on top of it:
class SplashScreen extends FullScreen
{
protected void onObscured()
{
close();
}
}
Add menu to the Screen for having option Gotohomescreen some thing like this. Just pop the active screen from the display stack an push your home screen from the menus's run() method. For efficiently. If you want to go back in your application you can pop the active screen and it will go to that screen from where you have come.Let suppose you have added a backButton you can just override the fieldchanged method of the button and in fieldchanged method do something like this.
if(field == backbutton) {
UiApplication.getUiApplication().popScreen(UiApplication.getUiApplication().getActiveScreen());
}
using the memory here is your answer
The screen at the top of the stack is the active screen that the BlackBerry device user sees. When a BlackBerry device application displays a screen, it pushes the screen to the top of the stack. When a BlackBerry device application closes a screen, it removes the screen off the top of the stack and displays the next screen on the stack, redrawing it as necessary. Each screen can appear only once in the display stack. The BlackBerry JVM throws a runtime exception if a Screen that the BlackBerry device application pushes to the stack already exists.
A BlackBerry device application must remove screens from the display stack when the BlackBerry device user finishes interacting with them so that the BlackBerry device application uses memory efficiently
I need to load and process many things before my app starts, so when I test it on my iPhone it's always killed by iOS because it hangs the iPhone for too much time.
I then decided to write a loading screen class for my Apps, something that shows immediatly a logo and a progress indicator (keeping it responsive to avoid being killed by iOS), while in a background a separate thread initializes all my ViewControllers and then closes the loading screen and shows the main window.
What is the best way to do it with MonoTouch?
Any suggestion is welcome.
Thank you.
This is how I do it:
In the FinishedLaunching method, initialize and add your splash view to the main window:
window.AddSubview(this.splashView);
After that, invoke your code that does all the stuff you want to do in a thread/async invocation. I usually use the ThreadPool. Remember to invoke on the main thread:
ThreadPool.QueueUserWorkItem(delegate {
this.BeginInvokeOnMainThread(delegate {
//Initialize stuff here
//...
//when done, add your initial view to the window and remove the splash view
//eg.:
//window.AddSubview(myController.View);
//this.splashView.RemoveFromSuperview();
});
});
// show the window, which only displays the splash view now and return
window.MakeKeyAndVisible();
return true;
A rough example, but I hope it helps.