I want to integrate an android back button functionality in appcelerator for a parent window and its subsequent child views. I have just a single window in the entire project. Other screens are the children of this parent window. Any suggestions?
There are 2 cases you might be interested in:
Over-ride the default behaviour of the back button press.
$.window.onBack = function () {
// run your code like change views or whatever you like
};
Use Window's onBack property to attach back button callback.
It will have you take control of what should happen when back button is pressed on that window.
If you just want to listen to back button press along with default behaviour of back button press, use Window's androidback event.
$.window.addEventListener('androidback', function (e){});
Note that this method will still close the window you are on, but will allow you to run some code on back button press.
I believe you are looking for method 1.
Note: Method 1 is only available after or > SDK 5.5.1.GA. It was a breaking change in 6.0.0.GA. So do read docs properly & put some tests on their functionality.
Added Code sample to simulate back button feature.
Alloy.Globals.trackingArray = [];
function addNewView(_controllerName, _args) {
var newView = Alloy.createController(_controllerName, _args).getView();
$.window.add(newView);
// add new view in tracking array.
Alloy.Globals.trackingArray.push(_controllerName);
}
// now use something like this whenever you remove any view using backpress
function onBackPress() {
// remove lastly added view
Alloy.Globals.trackingArray.pop(_controllerName);
// add last opened view to simulate back button feature
var lastAddedView = Alloy.Globals.trackingArray[Alloy.Globals.trackingArray.length - 1];
var tempView = Alloy.createController(lastAddedView).getView();
$.window.add(tempView);
}
This is the basis of how you can manage this feature. But do note a point here that you will need to take care of managing arguments passed to while creating a new view or controller. There are multiple ways to do it & it depends on your coding style. :)
Related
i am trying to make an E-Leaning app.
I have a submit button, that when pressed, sends the user's answer to a server to be evaluated.
During that evaluation, I want to display a loading indicator (I am simply using Android Studio's progress bar).
During the onViewCreated of my fragment, I set the onClickListener as follows:
submit_button.setOnClickListener {
submit_button.isClickable = false
loadingIndicator.visibility = View.VISIBLE
inputFragment.passDataToActivityAndEvaluate()
}
On press, a coroutine is launched that makes an html request. After that, I want to hide the loading spinner before navigating the child fragment
loadingIndicator.visibility = View.INVISIBLE
submit_button.isClickable = true
The idea sounded simple enough, however, I discovered that when trying to make any form of UI change in the OnClickListener, it only gets applied when the evaulation is already done and the app has navigated to the next fragment.
I am using a swipe button from com.ebanx:swipe-button library in my application and I wish to change the state of the swipe button to enable (based on the information recieved via another Bluetooth device) when I open the button's activity. ie: Without any user input I have to change swipe button's state to enable !
You can use toggleState()
SwipeButton mSwipeButton; = findViewById(R.id.my_swipe_button);
mSwipeButton.toggleState();
if you use an older version where toggleState is not available, use collapseButton(); or expandButton(); to collapse or expand the swipe button
There are two issues with the library you're using, first is coding bug, second is wrong documentation, but that's not the case.
to make the button active:
SwipeButton swipe_btn = findViewById(R.id.swipe_btn);
swipe_btn.setEnabled(true);
now by default, the button state is closed and you can change that in the xml file i.e the layout where you created the button, you will see something like below:
<com.ebanx.swipebtn.SwipeButton
app:initial_state="disable" //change to enable will make button open by default
app:has_activate_state="true"
/>
Finally to monitor the state of the button, you will have to listen to the state changes like below:
swipe_btn.setOnStateChangeListener(new OnStateChangeListener() {
#Override
public void onStateChange(boolean active) {
Toast.makeText(MainActivity.this, "IS "+active, Toast.LENGTH_LONG).show();
}
});
if active, the button is open, else it's close.
Note: When I say open, I mean the button is toggledOn, and when I say close, it means the other way round(toggleOff).
The bug here is that when you use swipe_btn.toggleState(); The button will be deactivated, meaning it will not even respond to click event which is not right, so the way around is to use the onStateChangeListener as I use it above so that when the button is open you can do something and when it's close you can still do anything.
Note: library version: 'com.ebanx:swipe-button:0.8.3'
I am new to iOS. I have a very similar requirement like my working Android project. The requirement is that in my LoginActivity onCreate(), I am checking for some condition and if it is true then I am launching my next Activity using an Intent.
I am trying to perform the same functionality form my iOS app. In my LoginViewController viewDidLoad(), after checking for some condition, I am calling [self performSegueWithIdentifier:#"myNextControllerSegue" sender:nil];.
But, my ViewController is not changing to next view controller. Any help would be appreciated.
Need to perform segue at right place.you are tried to loading another view before first one in hierarchy.So viewDidAppear is called you have a fully loaded view to modify.
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self performSegueWithIdentifier:#"myNextControllerSegue" sender:nil];
}
To remove that flickering, just hide the view in your viewWillApear method.
otherwise as quick search you can do that into main thread also like below
dispatch_async(dispatch_get_main_queue(), { () -> Void in
//perform segue
})
viewDidLoad: is not used for segue performing. You should call the segue in viewDidAppear:, where the view structure is already established.
On your Storyboard, click on your view controller, go to the "Connections inspector" part, and in "Triggered segues", drag the "+" button to the controller you want to reach.
Now click on your segue and go on "Attributes inspector" part, and set "myNextControllerSegue" for identifier.
I am trying to work out how to show the "up" arrow in Xamarin.Forms without a pushing a page onto the stack. I.E. I just want to perform an action when the back button is pressed. I am completely stuck on this so any help would be appreciated.
I have tried creating a custom renderer which handles a view property called DisplayHomeAsBack. Which in the renderer calls the following:
FormsAppCompatActivity context = ((FormsAppCompatActivity)Forms.Context);
Android.Support.V7.App.ActionBar actionBar = context.SupportActionBar;
if (actionBar != null)
{
actionBar.SetDisplayHomeAsUpEnabled(element.DisplayHomeAsBack);
}
Unfortunately it seems this does absolutely nothing, even though all online tutorials and stackoverflow question for android suggest this method.
The plan is that I can then use the "OnBackButtonPressed" override in MasterDetailPage, which should allow me to perform this action. Unfortunately displaying the back button has been the larger hurdle so far!
Any idea of a better way to do this or how I can get the current mechanism to work?
EDIT
I have created a project and uploaded it to this question on the Xamarin support forums, if it helps.
http://forums.xamarin.com/discussion/comment/186330#Comment_186330
Sorry to keep you waiting so long!
Warning that I did not actually run this code and changed it from my own so I would be surprised if it worked perfectly without some changes.
So below should add a back button where there was not one before (so like when there is not really a page to go back to) and then we will add a custom action to perform when it gets pressed.
I would suggest you push a new page onto the stack without using animation so it is transparent to the user and also makes all of this much simpler, but if you absolutely do not want to do that, the below method should work.
MainActivity:
//Use this to subscribe to the event which will create the back button
public override bool OnCreateOptionsMenu(IMenu menu) {
if(menu != null && App.AppMasterPage != null) { //You will need this to make sure you are on your MasterDetailPage, just store a global reference to it in the App class or where ever
Xamarin.Forms.MessagingCenter.Unsubscribe<string>(this, "CreateBackButton");
Xamarin.Forms.MessagingCenter.Subscribe<string>(this, "CreateBackButton", stringWeWillNotUse => { //Use this to subscribe to the event that creates the back button, then when you want the back button to show you just run Xamarin.Forms.MessagingCenter.Send<string>(this, "CreateBackButton")
ActionBar.DisplayOptions = ActionBarDisplayOptions.ShowTitle | ActionBarDisplayOptions.ShowHome | ActionBarDisplayOptions.UseLogo | ActionBarDisplayOptions.HomeAsUp; //You may need to play with these options to get it working but the important one is 'HomeAsUp' which should add the back button
});
} else {
Xamarin.Forms.MessagingCenter.Unsubscribe<string>(this, "CreateBackButton");
}
return base.OnCreateOptionsMenu(menu);
}
Now the next step is do do a custom action when it is pressed. I think you can either override OnBackPressed() or OnOptionsItemSelected() in MainActivity or maybe you can override the MasterDetailPage method. I am not sure.
Which ever one works for you, inside of that override, I would simply check to see if you are on your App.AppMasterPage like we did above, and if so, send a MessagingCenter message which your App.AppMasterPage has already subscribed to in order for it to handle the custom action.
If you get stuck let me know!
I know it sounds like a bit of a hack, but the best "solution" I have found so far is to add a page behind the current page (behind the root) so it is not visible. Then when the user presses the back button, handle it by removing that page.
In Sencha touch if I use navigation view i can get back button. This is pretty fine.
But what if user hit device backbutton? it is direct exiting the applicaiton. In my requirement it should not exit the application it has to go back to previous screen.How can i do this?.
You can handle hardware back button like this:
if (Ext.os.is('Android')) {
document.addEventListener("backbutton", Ext.bind(onBackKeyDown, this), false);
function onBackKeyDown(eve) {
eve.preventDefault();
//do something
alert('back button pressed');
}
}
I didn't find the instructions on the history support page that useful when trying to do this; I couldn't see anyway to use routes when dealing with a navigation view which can have a large stack of views on it at anytime.
If you just want the back button to work though, you can use the popstate and pushstate functions (see https://developer.mozilla.org/en-US/docs/DOM/Manipulating_the_browser_history for a reference). The idea is that you push a state when adding a view and pop it off when removing one. The physical back button on an Android phone, or the back button on a desktop browser effectively calls history.back(); so all you need to do is ensure that pressing the back button on the titlebar does the same, and it is that which triggers the nav view to pop.
To make use it work in Sencha Touch, I add the following to the main controller:
In refs I have references to the main view (an instance of Ext.navigation.View) and to its titlebar, from which you can hook onto the event of the back button e.g.:
refs: {
main: 'mainview',
mainTitleBar: 'mainview titlebar',
}...
I attach the following functions via the control config object..
control: {
main: {
push: 'onMainPush'
},
mainTitleBar: {
back: 'onBack'
},
...
These are defined as:
onMainPush: function(view, item) {
//do what ever logic you need then..
history.pushState();
},
onBack: function() {
history.back(); //will cause the onpopstate event to fire on window..
//prevent back button popping main view directly..
return false;
},
I then attach a function to execute when the state is popped via the init function of..
init: function() {
/* pop a view when the back button is pressed
note: if it's already at the root it's a noop */
var that = this;
window.addEventListener('popstate', function() {
that.getMain().pop();
}, false);
},
Now, pressing back on the titlebar, executes history.back(), this in turn fires the popstate event which then causes the main view to pop.
If you want to see this working on a real application, there is a (v. basic!) property finder app using this technique on github here.