How to pass Activity type to a method in Xamarin.Android? - android-activity

All my activities inherit from BaseActivity. In BaseActivity I have the following method:
protected void GoToPreviousActivity(Activity)
{
StartActivity(typeof(Activity));
Finish();
}
When I click the toolbar's back button of every activity I want to go back to the previous activity, like that:
toolbar.NavigationOnClick += delegate
{
this.GoToPreviousActivity(PreviousActivity);
};
How can I do that?

As #Mathias Kirkegaard commented, Android provides its own navigation back and it is very reliable (you can even manipulate the back stack (see the link at the bottom about what is it))
Having said that, if you want to use your method every time the user clicks on the back button you can override the OnBackPressed method, and provide your own implementation there.
In your case:
public override void OnBackPressed()
{
//base.OnBackPressed(); <-- this will use the default behaviour to navigate back, and if I understood correctly, you don't want to use it
GoToPreviousActivity(PreviousActivity);
}
Even though you can do that and it is valid, it is discouraged, you can read more about how Android manages the back navigation here:
https://developer.android.com/guide/components/activities/tasks-and-back-stack
and here is a good article on how to implement a custom back navigation: https://developer.android.com/guide/navigation/navigation-custom-back

should use OnBackPressed methode on click
base.OnBackPressed();

Related

Swift and watchkit: pushControllerWithName not being called at all

I have always used the pushControlledWithName method in swift/watchkit to move to another interface controller, basically like this:
self.pushControllerWithName("newinterfacecontroller", context: nil)
In some of my projects, when I put this in a function (like where the user presses a button) it simply doesn't get called at all. No errors, just as if the code isn't there at all. If I create a new test project and try it it works. I am baffled as to what's going on here.
Example of what happens:
#IBAction func button1Action() {
println("test")
self.pushControllerWithName("newinterfacecontroller", context: nil)
}
Pressing the button will print "test" in the console, but it doesn't try to move to the new interface controller (with identifier "newinterfacecontroller") at all.
I think you've figured this out from the comments, but page-based interfaces are technically modals and not navigation-stack interfaces.
You can present modals from anywhere, but you can only push onto a navigation stack from a non-modal.

Activate part warning

My application is a perspective with two views.
When starting it, the application shows viewA with a table, the viewB is hidden.
When I select an item from the table, opens the viewB, send the item you selected to viewB, and hides the viewA.
I can perform these actions but on the console I have the following warning:
"Prevented recursive attempt to activate part "viewB" while still in the middle of activating part "viewA".
Some help to solve this warning?.
Are you doing the work to display "viewB" inside the button click event method? If so, you probably need to queue that work up for the UI thread to do later on by wrapping it in:
Display.getDefault().asyncExec(new Runnable() {
#Override
public void run() {
// Your UI update code.
}
};

Moving view up to accommodate keyboard

I have a view with multiple text fields and I want to do the same effect that the Contacts application does when you click on a text field would otherwise be hidden by the keyboard when it comes up. When I dismiss the keyboard I plan on moving the view back down properly.
I suspect that I do this by changing the Frame value, but I need this to be animated so that it isn't jarring to the user.
Advice? Examples?
Wrapping your view in a UIScrollView is indeed the way to go. As well as on the textFieldDidEndEditing delegate, you could instead subscribe to the UIKeyboardDidHideNotification and UIKeyboardDidShowNotification and when you receive a notification that the keyboard did hide/show then scroll your view appropriately. I can post code examples for the keyboard notifications if you need it : )
Edit
Figured I'd post the code anyway - someone might find it helpful:
You need to declare listeners for the notifications:
NSObject hideObj = NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.DidHideNotification, HandleKeyboardDidHide);
NSObject showObj = NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.DidShowNotification, HandleKeyboardDidShow);
then your Action methods would look something like:
void HandleKeyboardDidShow(NSNotification notification)
{
scrollView.ScrollRectToVisible(textfield.Frame, true);
}
void HandleKeyboardDidHide(NSNotification notification)
{
// scroll back to normal
}
Edit 2
So if you'd like to remove the Observers when the view is destroyed, first you need to ensure you assign NSObjects when adding the observers then use the following code to remove them:
NSNotificationCenter.DefaultCenter.RemoveObserver(showObj);
NSNotificationCenter.DefaultCenter.RemoveObserver(hideObj);
Hope that helps.
I just did this on an application. I used a scrollview to wrap my entire view, and then used scrollToRectVisible on the textFieldDidEndEditing-delegate method. It worked perfectly!
The Apple documentation on about the keyboard management topic is pretty good and contains code (at the bottom) for most situations that that you can copy/paste right into your app.
Best of luck.

Android's viewDidLoad and viewDidAppear equivalent

Does Android have an equivalent to Cocoa's viewDidLoad and viewDidAppear functions?
If not, then how would I go about performing an action when a View appears? My app is a tabbed application, in which one of the tabs is a list of forum topics. I would like the topic list to be refreshed every time the view appears. Is such a thing possible in Android?
The Activity class has onCreate and onResume methods that are pretty analagous to viewDidLoad and viewDidAppear.
Activity.onResume
EDIT
To add to this, since some have mentioned in the comments that the view tree is not yet fully available during these callbacks, there is the ViewTreeObserver that you can listen to if you need first access to the view hierarchy. Here is a sample of how you can use the ViewTreeObserver to achieve this.
View someView = findViewById(R.id.someView);
final ViewTreeObserver obs = someView.getViewTreeObserver();
obs.addOnPreDrawListener(new OnPreDrawListener() {
public boolean onPreDraw() {
obs.removeOnPreDrawListener(this);
doMyCustomLogic();
return true;
}
});
onResume() is more like viewCouldAppear. :) public void onWindowFocusChanged(boolean) is the closest to viewDidAppear. At this point within the activity lifecycle you may ask the view about its size.
From my limited, nascent understanding of Android, you implement viewDidLoad type functionality in the onCreate method of your Activity:
onCreate(Bundle) is where you
initialize your activity. Most
importantly, here you will usually
call setContentView(int) with a layout
resource defining your UI, and using
findViewById(int) to retrieve the
widgets in that UI that you need to
interact with programmatically.
The equivalent for viewDidAppear is closer to the onResume method:
Called after
onRestoreInstanceState(Bundle),
onRestart(), or onPause(), for your
activity to start interacting with the
user. This is a good place to begin
animations, open exclusive-access
devices (such as the camera), etc.

Delegation, some example of code? How object delegate to other

I would like to gain a better understanding about the delegation. Can somebody please paste a good code sample of delegation and explain how it works?
There is a pretty good example at: http://en.wikipedia.org/wiki/Delegation_pattern#Objective-C_example
In this example, MyCoolAppController creates and object of type TCScrollView, and sets the "delegate" property of the TCScrollView to self. This means that when the TCScrollView calls
[delegate scrollView:self shouldScrollToPoint:to]
it is asking the MyCoolAppController (the delegate of the TCScrollView) to perform some calculations and see if it is ok to scroll. You can say "MyCoolAppController is the delegate of TCScrollView" to describe this; TCScrollView asks MyCoolAppController to do some work on its behalf.
Do you mean .NET or Java or some other language delegate?
A delegate in .NET parlance is nothing more than a function pointer, or in other words a variable that points to a block of executable code. They can be used in may ways. One way is to use them in the context of events. Lets say you have an ASP.NET page and you are using the MVP (Model View Presenter pattern on that page). You want your presenter to be notified of the click event of the save button on the view. You can define an event on the views interface, but in order to subscribe to that event and to take action on it you need to register a method that gets fired when the event is raised. For example:
public class ClassThatRegistersForEvent
{
public void InitializeView(IView view)
{
view.SaveButtonClickedEvent += delegate{
// do stuff in here when the event is raised
}
}
}
public interface IView
{
event System.EventHandler SaveButtonClickedEvent;
}
Here's an answer I wrote explaining delegation: https://stackoverflow.com/questions/1089737#1090170
A delegate is a way to respond to events. In other languages you would probably do this by subclassing. For example, say you have a table view. You could subclass the tableview and override the tableView:didSelectRowAtIndexPath: method, but that would get messy and create an unnecessary subclass (along with the fact that its not reusable) Instead, you create a TableViewDelegate class and tell your table view about it (tableView.delegate). This way, the method will automatically get called when something happens. This is a really clean solution to event-handling.
After you write a few apps that involve delegates (table views are the big ones), you'll get the hang of it.