I have created a very basic ios project that has a single button on it with it's title text property set to "Start". I am trying to have this button count down from 15.
I have added the event handler for the button as below.
void StartButton_TouchUpInside (object sender, EventArgs ea) {
_currentCount = 15;
_timer = NSTimer.CreateRepeatingScheduledTimer(TimeSpan.FromSeconds(1), () =>
{
if (_currentCount <= 0) {
StartButton.TitleLabel.Text = "Start";
_timer.Dispose();
} else {
string titleText = (_currentCount--).ToString ();
//Console.WriteLine(titleText);
StartButton.TitleLabel.Text = titleText;
//Console.WriteLine(StartButton.TitleLabel.Text);
//StartButton.SetNeedsDisplay();
}
});
}
So here is the strange thing. The Button changes to 15 just fine then it flashes back to "Start" then 14. Then 13 then "Start" then 12 etc... etc... etc...
Why is it doing this? And how do I prevent it from flashing back to start. Thanks in advance.
Try to use this.InvokeOnMainThread
private int counter = 0;
public override void WillActivate()
{
Console.WriteLine ("{0} will activate", this);
_timer = NSTimer.CreateRepeatingScheduledTimer(1, (timer) =>
{
this.InvokeOnMainThread(()=>{
Console.WriteLine("++++++++ NSTimer Call");
this.AlertTimeLabel.SetText(counter.ToString());
counter++;
});
});
}
Related
I'm new to Mobile Development
I am currently developing a Telephone Directory app from Xamarin Android, in this app, I have 4 Fragment(HomeFragment, AboutFragment, DirectoryFragment, and SyncFragment).
HomeFragment is the first fragment that shows from the app. When I click the item About in the side bar then pressed the back button, it works well because i used AddToBackStack(null) before commit() in the main activity.
But the problem is, let's say I open the app then it shows the HomeFragment fist by default, when i navigate to the AboutFragment, it shows the AboutPage, then if I navigate to the DirectoryFragment next and click the back button, i keep on going back to the AboutPage/AboutFragment, which is what i want to achieve is, it should go back to the HomePage/HomeFragment
In short, what i want to achieve is like the Navigation behavior of the Gmail App.
Anyway this is my code in Main Activity
switch (e.MenuItem.ItemId)
{
case (Resource.Id.nav_home):
FragmentTransaction ft = FragmentManager.BeginTransaction();
HomeFragment home = new HomeFragment();
ft.Replace(Resource.Id.HomeFrameLayout, home);
HideSoftKeyboard();
mDrawerLayout.AddDrawerListener(mDrawerToggle);
ft.AddToBackStack(null);
ft.Commit();
break;
case (Resource.Id.nav_about):
FragmentTransaction ft1 = FragmentManager.BeginTransaction();
AboutFragment about = new AboutFragment();
ft1.Replace(Resource.Id.HomeFrameLayout, about);
HideSoftKeyboard();
ft1.AddToBackStack(null);
ft1.Commit();
break;
case (Resource.Id.nav_etel):
FragmentTransaction ft2 = FragmentManager.BeginTransaction();
GHQFragment ghq = new DirectoryFragment();
ft2.Replace(Resource.Id.HomeFrameLayout, ghq);
HideSoftKeyboard();
ft2.AddToBackStack(null);
ft2.Commit();
break;
case (Resource.Id.nav_refresh):
if (CrossConnectivity.Current.IsConnected)
{
FragmentTransaction ft3 = FragmentManager.BeginTransaction();
SyncFragment sync = new SyncFragment();
ft3.Replace(Resource.Id.HomeFrameLayout, sync);
HideSoftKeyboard();
ft3.AddToBackStack(null);
ft3.Commit();
}
else
{
Toast.MakeText(this, "Please connect to the internet to sync records.", ToastLength.Long).Show();
}
break;
You can override the OnKeyDown method like following code.
public override bool OnKeyDown([GeneratedEnum] Keycode keyCode, KeyEvent e)
{
if (keyCode == Keycode.Back)
{
FragmentManager fragmentManager = this.FragmentManager;
int count = fragmentManager.BackStackEntryCount;
for (int i = 0; i < count; ++i)
{
fragmentManager.PopBackStack();
}
// your code
return false;
}
return base.OnKeyDown(keyCode, e);
}
I switch three fragment, when I click the back button. it switch to the first fragment.
Update
Do you want to achieve the result that you click back button then back to the desktop when you in the HomePage?
Here is code.
public override bool OnKeyDown([GeneratedEnum] Keycode keyCode, KeyEvent e)
{
if (keyCode == Keycode.Back)
{
FragmentManager fragmentManager = this.FragmentManager;
int count = fragmentManager.BackStackEntryCount;
if(count>0){
for (int i = 0; i < count; ++i)
{
fragmentManager.PopBackStack();
}
return false;
}
}
return base.OnKeyDown(keyCode, e);
}
I want my splash screen to always appear in my application and it does which is great, but I have a walk through after the splash screen and I want it to be a one time walk through, So i want to add an integer to the shared preferences with a value of 0 and everytime I open the splash screen the value is incremented by one, so when "number" equals 1 or greater at the second run the splash screen skips the walkthrough and goes to home , here is the code that I want to edit now :
void initState() {
// TODO: implement initState
super.initState();
Timer(Duration(seconds: 5), () => MyNavigator.goToIntro(context));
}
And I want it to be like :
void initState() {
// TODO: implement initState
super.initState();int number=0;//this is in the shared prefs
Timer(Duration(seconds: 5), () => if(number==0){MyNavigator.goToIntro(context));
}else{MyNavigator.goToHome(context));
number++;}
}
The below code prints perfectly as we expect(during first launch only, "First launch"). You can use your navigation logic instead of print.
#override
void initState() {
super.initState();
setValue();
}
void setValue() async {
final prefs = await SharedPreferences.getInstance();
int launchCount = prefs.getInt('counter') ?? 0;
prefs.setInt('counter', launchCount + 1);
if (launchCount == 0) {
print("first launch"); //setState to refresh or move to some other page
} else {
print("Not first launch");
}
}
We need to have the number value to be saved across multiple app launches. We can use shared_preference plugin to achieve this.
secondly, getData that saved in our device.
Future<bool> getSaveData() async {
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
bool isIntroScreenOpenedBefore =
sharedPreferences.getBool("isIntroScreenOpened") ?? false;
print(sharedPreferences.containsKey("isIntroScreenOpened")); // check your key either it is save or not?
if (isIntroScreenOpenedBefore == true) {
Navigator.push(context, MaterialPageRoute(builder: (context) {
return LoginBoard();
}));
} else {
Navigator.push(context, MaterialPageRoute(builder: (context) {
return WalKThroughScreen();
}));
}
return isIntroScreenOpenedBefore;
}
at first, let's save the data as boolean
Future<void> saveData() async {
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
bool isIntroScreenOpened = true;
sharedPreferences.setBool("isIntroScreenOpened", isIntroScreenOpened); // saved data to your device.
}
Answer by #Dinesh Balasubramanian is works really fine.
But I have 4 initial screen that need to show once. I have done that using same logic in each screen. and then my app was showing 5th screen second time like fast forwarding all the previous screen and stopping on 5th screen.
To resolve this I am getting all the set Preferences at main.dart to open directly 5th screen. but when I do that I am having this problem,
"E/flutter (32606): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)]
Unhandled Exception: Navigator operation requested with a context that
does not include a Navigator.
E/flutter (32606): The context used to
push or pop routes from the Navigator must be that of a widget that is
a descendant of a Navigator widget."
Here is code to switch from main.dart:
int firstLogin, firstMobile, firstOtp, firstInfo;
void setValue() async {
final prefs = await SharedPreferences.getInstance();
firstLogin = prefs.getInt('counterLogin') ?? 0;
firstMobile = prefs.getInt('counterMobile') ?? 0;
firstOtp = prefs.getInt('counterOtp') ?? 0;
firstInfo = prefs.getInt('counterInfo') ?? 0;
prefs.setInt('counterLogin', firstLogin + 1);
prefs.setInt('counterMobile', firstMobile + 1);
prefs.setInt('counterOtp', firstOtp + 1);
prefs.setInt('counterInfo', firstInfo + 1);
if ((firstLogin == 0) && (firstMobile == 0) && (firstOtp == 0) && (firstInfo == 0)) {
setState(() {
print("first launch");
Navigator.of(context).pushNamed(LoginScreen.routeName);
});
} else {
setState(() {
print("not first launch");
Navigator.of(context).pushNamed(LandingSection.routeName);
});
}
}
And calling the setValue() in initState()
I am looking forward for solution.
How can i pause application for prevention from running next method unit client does not selected dialog buttons?
For example i am showing location update dialog for accessing location service and i want to pause my application for dialog response
public CLLocation UpdateUserLocation()
{
CLLocation currentLocation = null;
CLLocationManager LocMgr = new CLLocationManager();
if (CLLocationManager.LocationServicesEnabled)
{
if (UIDevice.CurrentDevice.CheckSystemVersion (6, 0))
{
LocMgr.LocationsUpdated += (object sender, CLLocationsUpdatedEventArgs e) =>
{
currentLocation = e.Locations [e.Locations.Length - 1];
};
}
else
{
LocMgr.UpdatedLocation += (object sender, CLLocationUpdatedEventArgs e) =>
{
currentLocation = e.NewLocation;
};
}
LocMgr.StartUpdatingLocation ();
LocMgr.Failed += (object sender, NSErrorEventArgs e) =>
{
Console.WriteLine (e.Error);
};
}
else
{
currentLocation = null;
Console.WriteLine ("Location services not enabled, please enable this in your Settings");
}
if (currentLocation != null)
{
LocationDetector.Instance.UpdateCurrentArea (new MyLatLng (currentLocation.Coordinate.Latitude, currentLocation.Coordinate.Longitude));
}
return currentLocation;
}
If I am understanding your question correctly.
When you display a dialog box, you are wanting to stop execution of the current method from further executing until the user selects a dialog box response.
Once they have selected a response, you would then like to continue execution of the code in the same function, effectively achieving your 'pause' that you are after.
To achieve this in iOS you can use a TaskCompletionSource.
In the example below it shows a dialog box first, asking the user if they want some coffee and then waits for the user to respond.
Once the user responds, it then continues execution, within the same function, and displays a further message box that is dependent on the selection that the user made.
UIButton objButton1 = new UIButton (UIButtonType.RoundedRect);
objButton1.SetTitle ("Click Me", UIControlState.Normal);
objButton1.TouchUpInside += (async (o2, e2) => {
int intCoffeeDispenserResponse = await ShowCoffeeDispenserDialogBox();
//
switch (intCoffeeDispenserResponse)
{
case 0:
UIAlertView objUIAlertView1 = new UIAlertView();
objUIAlertView1.Title = "Coffee Dispenser";
objUIAlertView1.Message = "I hope you enjoy the coffee.";
objUIAlertView1.AddButton("OK");
objUIAlertView1.Show();
break;
case 1:
UIAlertView objUIAlertView2 = new UIAlertView();
objUIAlertView2.Title = "Coffee Dispenser";
objUIAlertView2.Message = "OK - Please come back later when you do.";
objUIAlertView2.AddButton("OK");
objUIAlertView2.Show();
break;
}
});
//
View = objButton1;
private Task<int> ShowCoffeeDispenserDialogBox()
{
TaskCompletionSource<int> objTaskCompletionSource1 = new TaskCompletionSource<int> ();
//
UIAlertView objUIAlertView1 = new UIAlertView();
objUIAlertView1.Title = "Coffee Dispenser";
objUIAlertView1.Message = "Do you want some coffee?";
objUIAlertView1.AddButton("Yes");
objUIAlertView1.AddButton("No");
//
objUIAlertView1.Clicked += ((o2, e2) => {
objTaskCompletionSource1.SetResult(e2.ButtonIndex);
});
//
objUIAlertView1.Show();
//
return objTaskCompletionSource1.Task;
}
For some reason, when trying to activate the button programmatically via the Stop() method, the button image does not change back to the image associated with the normal state until I click on the button again with the mouse. Any ideas?
public void Stop()
{
buttonStartStop.SendActionForControlEvents(UIControlEvent.TouchUpInside);
}
partial void actionButtonStartStopPress(MonoTouch.Foundation.NSObject sender)
{
Console.WriteLine("StartStop Button State On Entering Handler: " + buttonStartStop.State);
if(buttonStartStop.State == UIControlState.Highlighted)
{
buttonStartStop.Selected = true;
buttonStartStop.Highlighted = true;
buttonLiveHome.Enabled = false;
buttonLiveBack.Enabled = false;
buttonCalibrate.Enabled = false;
MainLoop.StreamData(true);
}
else
{
buttonStartStop.SetTitle("Start", UIControlState.Normal);
buttonStartStop.Selected = false;
buttonStartStop.Highlighted = false;
buttonLiveHome.Enabled = true;
buttonLiveBack.Enabled = true;
buttonCalibrate.Enabled = true;
MainLoop.StreamData(false);
}
Console.WriteLine("StartStop Button State On Exiting Handler (0 means Normal): " + buttonStartStop.State);
}
You must send the action from the main thread for the button.
public void Stop()
{
buttonStartStop.InvokeOnMainThread (new NSAction (()=> {
buttonStartStop.SendActionForControlEvents(UIControlEvent.TouchUpInside);
}));
}
When I run my method from the constructor of the mainscreen it works, but if invoked in the timer then I get the error.
Here is my method:
public void buildDesc(){
try {
JSONObject event = array.getJSONObject(currentPage);
String title = event.getString("title");
String by = event.getString("by");
String by_name = event.getString("by_name");
String summary = event.getString("summary");
int nid = event.getInt("nid");
vfm.add(new LabelField(title));
System.out.println("The Title:"+title);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
And the timer:
timer = new Timer();//Create the timer to loop the events every 5 seconds
timer.scheduleAtFixedRate(new TimerTask(){
public void run() {
currentPage++;
if(currentPage > 3){
currentPage = 0;
}
System.out.println("Page Position:"+pagePosition(currentPage+1));
gallery.setHorizontalScroll(pagePosition(currentPage));
buildDesc();
}
}, 0, 10000);
I read a Android question that says, perhaps, I can't make changes to the UI if not on the UIThread?
I think your hunch is right, in BlackBerry you shouldn't do any work on the Ui thread. I guess the Timer thread is getting terminated for it's behaviour which is why you are getting an IllegalStateException. A quick guide to the UI thread can be found here.
Try :
timer = new Timer();//Create the timer to loop the events every 5 seconds
timer.scheduleAtFixedRate(new TimerTask(){
public void run() {
currentPage++;
if(currentPage > 3){
currentPage = 0;
}
System.out.println("Page Position:"+pagePosition(currentPage+1));
synchronized(UiApplication.getUiApplication().getEventLock())) {
// UI Code here
gallery.setHorizontalScroll(pagePosition(currentPage));
buildDesc();
}
}
}, 0, 10000);
Note: The above is untested.