How to show the 1st fragment when back button is pressed? - xamarin.android

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);
}

Related

Invite Friend in Google Play Services

I'm creating a Game App in objective-c which is using Google Play Game services for realtime Multiplayer functionality. I follows the documentation at https://developers.google.com/games/services/ios/turnbasedMultiplayer. In my app there are two options Auto match and Invite Match. Auto Match functionality working fine. But Invite match not.
I follow following Code for this
- (int)minPlayersForPlayerPickerLauncher {
return 1;
}
- (int)maxPlayersForPlayerPickerLauncher {
return 2;
}
- (IBAction)inviteFriendsWasPressed:(id)sender
{
// This can be a 2-4 player game
[GPGLauncherController sharedInstance].playerPickerLauncherDelegate = self;
// This assumes your class has been declared a GPGPlayerPickerLauncherDelegate
[[GPGLauncherController sharedInstance] presentPlayerPicker];
}
on click this button Action follow Screen is open
See here
After that when I enter emailId in textfield there is no action perform to search particular user.
Please help me
Thanks
Unfortunately, the player selection no longer works since Google+ is no longer integrated into Play Game Services: https://android-developers.googleblog.com/2016/12/games-authentication-adopting-google.html
// request code for the "select players" UI
// can be any number as long as it's unique
final static int RC_SELECT_PLAYERS = 10000;
// launch the player selection screen
// minimum: 1 other player; maximum: 3 other players
Intent intent = Games.RealTimeMultiplayer.getSelectOpponentsIntent(mGoogleApiClient, 1, 3);
startActivityForResult(intent, RC_SELECT_PLAYERS);
#Override
public void onActivityResult(int request, int response, Intent data) {
if (request == RC_SELECT_PLAYERS) {
if (response != Activity.RESULT_OK) {
// user canceled
return;
}
// get the invitee list
Bundle extras = data.getExtras();
final ArrayList<String> invitees =
data.getStringArrayListExtra(Games.EXTRA_PLAYER_IDS);
// get auto-match criteria
Bundle autoMatchCriteria = null;
int minAutoMatchPlayers =
data.getIntExtra(Multiplayer.EXTRA_MIN_AUTOMATCH_PLAYERS, 0);
int maxAutoMatchPlayers =
data.getIntExtra(Multiplayer.EXTRA_MAX_AUTOMATCH_PLAYERS, 0);
if (minAutoMatchPlayers > 0) {
autoMatchCriteria = RoomConfig.createAutoMatchCriteria(
minAutoMatchPlayers, maxAutoMatchPlayers, 0);
} else {
autoMatchCriteria = null;
}
// create the room and specify a variant if appropriate
RoomConfig.Builder roomConfigBuilder = makeBasicRoomConfigBuilder();
roomConfigBuilder.addPlayersToInvite(invitees);
if (autoMatchCriteria != null) {
roomConfigBuilder.setAutoMatchCriteria(autoMatchCriteria);
}
RoomConfig roomConfig = roomConfigBuilder.build();
Games.RealTimeMultiplayer.create(mGoogleApiClient, roomConfig);
// prevent screen from sleeping during handshake
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
}
// create a RoomConfigBuilder that's appropriate for your implementation
private RoomConfig.Builder makeBasicRoomConfigBuilder() {
return RoomConfig.builder(this)
.setMessageReceivedListener(this)
.setRoomStatusUpdateListener(this);
}

Printing from Win RT Application (Windows 8.1 App)

I am developing a POS application in windows 8.1 (Universal App).
Order receipt will be printed from the application and even I am able to do so.
Printer - EPSON TM-U220D
Input - A grid is being created pragmatically with dynamic content within the ViewModel. So this grid is the input for printer
Output - In the print preview (Attahced pic 1) all looks good but when the receipt is actually printed then content is cut off from the end (Attahced pic 2)
PIC 1
PIC 2
Observations -
If I print a normal text file manually using print command (Right click file and then print), then all content is printed perfectly.
If I print that SAME content, from the application, by creating Dynamic grid, then with Small font size print is good but with bit bigger font size content again cuts off.
Tried -
Optimized the code for Creating Gird, by specifying height
Questions-
If preview is all good then why not output
Did anyone tried using ePOS-Print_SDK_141020E for Windows Store app?
Generate Dynamic Grid Code
private void AddRows(Grid grid, int count)
{
for (int i = 0; i < count; i++)
{
RowDefinition row = new RowDefinition();
row.Height = GridLength.Auto;
grid.RowDefinitions.Add(row);
}
}
private void AddColumns(Grid grid, int count)
{
for (int i = 0; i < count; i++)
{
grid.ColumnDefinitions.Add(new ColumnDefinition());
}
}
private TextBlock CreateTextBlock(string text, Color color, FontWeight fw, double fs = 10, int thick = 5)
{
if (color == null) color = Colors.Black;
TextBlock txtBlock1 = new TextBlock();
txtBlock1.Text = text;
txtBlock1.FontSize = fs;
txtBlock1.FontWeight = fw;
txtBlock1.Foreground = new SolidColorBrush(color);
txtBlock1.VerticalAlignment = VerticalAlignment.Center;
txtBlock1.Margin = new Thickness(thick);
return txtBlock1;
}
private async Task<Grid> CreateDynamicWPFGrid()
{
Grid ParentGrid = new Grid();
AddRows(ParentGrid, 8);
/* Start First Grid*/
Grid DynamicGrid = new Grid();
DynamicGrid.Width = 230;
DynamicGrid.HorizontalAlignment = HorizontalAlignment.Left;
DynamicGrid.VerticalAlignment = VerticalAlignment.Top;
DynamicGrid.Margin = new Thickness(24, 0, 0, 0);
AddColumns(DynamicGrid, 2);
AddRows(DynamicGrid, 3);
TextBlock txtBlock1 = CreateTextBlock(DateTime.Now.ToString("M/d/yy"), Colors.Black, FontWeights.Normal);
Grid.SetRow(txtBlock1, 0);
Grid.SetColumn(txtBlock1, 1);
.
.
.
.
Return ParentGrid;
}
Printer Events Code
//Register Print Contract
async Task RegisterPrintContract()
{
PrintManager manager = PrintManager.GetForCurrentView();
manager.PrintTaskRequested += OnPrintTaskRequested;
await PrintManager.ShowPrintUIAsync();
}
//Unregister Print Contract
void UnregisterPrintContract()
{
PrintManager printMan = PrintManager.GetForCurrentView();
printMan.PrintTaskRequested -= OnPrintTaskRequested;
}
void OnPrintTaskRequested(PrintManager sender, PrintTaskRequestedEventArgs args)
{
// If I need to be asynchronous, I can get a deferral. I don't *need*
// to do this here, I'm just faking it.
var deferral = args.Request.GetDeferral();
PrintTask printTask = args.Request.CreatePrintTask("My Print Job", OnPrintTaskSourceRequestedHandler);
printTask.Completed += OnPrintTaskCompleted;
deferral.Complete();
}
void OnPrintTaskCompleted(PrintTask sender, PrintTaskCompletedEventArgs args)
{
// TODO: Tidy up.
this._document = null;
this._pages = null;
}
async void OnPrintTaskSourceRequestedHandler(PrintTaskSourceRequestedArgs args)
{
var deferral = args.GetDeferral();
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
this._document = new PrintDocument();
this._document.Paginate += OnPaginate;
this._document.GetPreviewPage += OnGetPreviewPage;
this._document.AddPages += OnAddPages;
// Tell the caller about it.
args.SetSource(this._document.DocumentSource);
});
deferral.Complete();
}
void OnAddPages(object sender, AddPagesEventArgs e)
{
// Loop over all of the preview pages and add each one to add each page to be printied
// We should have all pages ready at this point...
foreach (var page in this._pages)
{
//this._pages[page.Key]
this._document.AddPage(this._pages[page.Key]);
}
PrintDocument printDoc = (PrintDocument)sender;
// Indicate that all of the print pages have been provided
printDoc.AddPagesComplete();
}
async void OnGetPreviewPage(object sender, GetPreviewPageEventArgs e)
{
Grid x = await CreateDynamicWPFGrid();
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
// NB: assuming it's ok to keep all these pages in
// memory. might not be the right thing to do
// of course.
if (this._pages == null)
{
this._pages = new Dictionary<int, UIElement>();
}
if (!this._pages.ContainsKey(e.PageNumber))
{
this._pages[e.PageNumber] = x;
}
if (this._document == null)
this._document = new PrintDocument();
this._document.SetPreviewPage(e.PageNumber, this._pages[e.PageNumber]);
}
);
}
async void OnPaginate(object sender, PaginateEventArgs e)
{
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
// I have one page and that's *FINAL* !
this._document.SetPreviewPageCount(e.CurrentPreviewPageNumber, PreviewPageCountType.Final);
});
}

Updating a Button control with an NSTimer in Xamarin

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++;
});
});
}

Monotouch : pause application unit dialog response

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;
}

Coding4Fun MessagePrompt not displayed when app is on the splash screen

I am using coding4fun message prompt in my app to display message box with customized buttons to the user. It works fine when the app is in the foreground and there is a message to be displayed. But, When I exit the app and re-launch it there are a set of things happening when after app's splash screen is displayed and before the main screen of the app is shown. During this process the app checks if any upgrade is available, if there are upgrades then on the splash screen the message box must be displayed to the user. When i was previously using xna framework message box this worked well, but due to marketplace submission process I replaced xna framework message box with coding4fun message prompt and the message prompt doesn't show when splash screen is displayed. Is this how Coding4Fun message prompt works or am i doing something wrong?
here is how i have implemented the message prompt in my code..
public MainPage()
{
System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
{
DataManager.getInstance().setUIListener(this);
if (checkUpgradeStatus())
{
return;
}
});
}
private bool checkUpgradeStatus()
{
try
{
string flag = "1";
if (!Utils.isNullString(flag))
{
DataManager.getInstance().CheckForUpgrade();
if (flag.Equals(CacheManager.MAJOR_UPGRADE))
{
customizedMessageBox(Utils.APP_UPGRADE_CONFIRM, CustomizedMessageBox.APP_UPGRADE_CONFIRM_TITLE, AppResources.APP_UPGRADE_CONFIRM);
DataManager.getInstance().UpdateBadge(true);
return true;
}
else if (flag.Equals(CacheManager.MINOR_UPGRADE))
{
CacheManager.getInstance().writeDataToConfigFile(CacheManager.APP_UPGRADE_STATUS, "0");
customizedMessageBox(Utils.APP_MINOR_UPGRADE_CONFIRM, CustomizedMessageBox.APP_MINOR_UPGRADE_CONFIRM_TITLE, AppResources.APP_UPGRADE_MINOR_CONFIRM);
DataManager.getInstance().UpdateBadge(true);
return false;
}
}
CacheManager.getInstance().writeDataToConfigFile(CacheManager.APP_UPGRADE_STATUS, "0");
return false;
}
catch (Exception ex)
{
Logger.log(TAG, ":checkUpgradeStatus():" + ex.Message);
return false;
}
}
public void customizedMessageBox(int messageboxtype, string title, string text)
{
try
{
switch (messageboxtype)
{
Case 6:
messageBox = new MessagePrompt();
Button btnMinorUpgrade = new Button();
btnMinorUpgrade.Content = "Upgrade";
messageBox.ActionPopUpButtons.Add(btnMinorUpgrade);
btnMinorUpgrade.Click += new RoutedEventHandler(btnMinorUpgrade_Click);
Button btnMinorUpgradeCancel = new Button();
btnMinorUpgradeCancel.Content = "Cancel";
messageBox.ActionPopUpButtons.Add(btnMinorUpgradeCancel);
btnMinorUpgradeCancel.Click += new RoutedEventHandler(btnMinorUpgradeCancel_Click);
messageBox.Show();
break;
}
}
catch (Exception ex)
{
Logger.log(TAG, ":customizedMessageBox():" + ex.Message);
}
}
NOTE*:- For testing purpose I have hard-coded flag's value to "1".
All the answers and suggestions appreciated.
Thank you

Resources