Sunday, March 31, 2013

Handling the Device Back button in ADF Mobile Application

While developing an ADF Mobile application for Android/IOS, you may require to capture the device back button on user click. Here we will add little logic to exit the application once the user clicks on device back button.

We can directly use the phone gap backbutton api to override the default back button behaviour, you can register an event listener for the 'backbutton' event. It is no longer necessary to call any other method to over ride the back button behaviour. Now, you only need to register an event listener for 'backbutton'.

Application screen looks like below when it is deployed and run on the Android Device/Emulator. Click on the device back button will launch the confirm dialog, user can click OK to exit the application.


You can download the sample workspace from here. I have checked in Android Device/Emulator and it's working fine, not sure in IOS device.

Implementation Steps

Create an ADF Mobile Application, the application consists of two projects. Application Controller project of Application LifeCycle, Listeners, Device Features DataControl and ViewController project contains mobile features content like AMX Files, Task Flows and DataControl.

Right click on the ViewController project and in New Gallery window, select Web Tier-> HTML -> JavaScript File and name as "index.js". Next locate and expand the Application Sources folder, then expand the META-INF folder. You will see the adfmf-feature.xml file, click on the adfmf-feature.xml file to launch the Feature editor. Add a new feature by clicking the green plus sign on the Features table near top of the editor this will launch the new Create ADF Mobile Feature dialog, leave the default values and click ok.

In the Features table, select the newly created feature "feature1". Under the Features table, click the Content tab, and locate the Content table. Notice that the content item feature1.1 is created by default. Next add a new file by clicking the green plus sign and select ADF Mobile AMX Page, this will launch the new Create ADF Mobile AMX Page dialog, modify the File Name as index.amx.

Next in Includes section, click the green plus sign and Insert Include the javascript file as shown below.

Open the index.js file and below code catches the event that fires when the user presses the device back button.
//This is an event that fires when the user presses the device back button
document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() {
    document.addEventListener("backbutton", backKeyDown, true);
}

function backKeyDown() {
    var cFirm = confirm("Are you sure you want to exit the application?");
    if (cFirm == true) {
        //Code to exit the application
        navigator.app.exitApp();
    }
}

79 comments:

  1. Thank you for the post, it was very helpful. Unfortunately, that doesn't seem to work when the feature is pointing to a taskflow, not to an amx page. Any ideas? Thanks!

    ReplyDelete
    Replies
    1. Hi,

      This work fine when the feature is pointing to a taskflow also. Please see the http://deepakcs.blogspot.in/2013/04/sync-device-back-button-with-adf-mobile.html article, I have explained back button with taskflow scenario.

      -Deepak

      Delete
  2. Thank you for the post, its very informative. I like it.

    ReplyDelete
  3. Loved this post. Thanks for sharing your day!

    ReplyDelete
  4. Hi,
    How can I only assign a JS file to only one amx page? but my amx page is in a taskflow but I dont want to apply this behavior to all pages in a taskflow?

    ReplyDelete
    Replies
    1. Hi Diego,

      //Check the device back button action happened in particular amx page
      //Here "Id" is the amx:panelPage Id
      if ($('#Id').length) {
      //To do code
      }

      You can check this article - http://deepakcs.blogspot.sg/2013/04/sync-device-back-button-with-adf-mobile.html

      - Deepak

      Delete
  5. Thanks for sharing such types of informative blog. can you detailed summarized about Mobile Applications in next your blog publish.


    ReplyDelete
  6. Thanks for the post but If i want the application to be removed from the memory and closed if i've pressed the Home button.
    like banking application i want it to be removed from the momory

    ReplyDelete
  7. I appreciate from this post thanks for sharing with us. Its seems looking so good.

    ReplyDelete
  8. I am awed by the nature of data on Marketing Your Mobile App. There are a ton of great assets here. I am certain I will visit this online journal again soon.windows mobile app // iPhone app maker // mobile app developers

    ReplyDelete
  9. Hi again!
    How can I invoke a java method from that javascript method?
    I have a commandbutton which goes back to the previous page, but I would like to do that using the nativa back button also.
    Thank you!

    ReplyDelete
  10. Diego,

    You can call the java method in javascript like below:

    adf.mf.api.invokeMethod("mobile.TestBean", "sayHello", onSuccess, onFail);

    mobile - PackageName
    TestBean - Managed Bean
    sayHello - Method in the managed bean
    onSuccess, onFail - Javascript callback functions

    ReplyDelete
    Replies
    1. Deepak,

      I did that and it called the java method, BUT now I need that button to go back to the previous page I tried returning "__back" but it didn't work any ideas on how to use the device native back button to do that?

      Delete
    2. I soon as I posted this: I found the solution here: http://goo.gl/loUA1U
      Using: adf.mf.api.amx.doNavigation(“__back”);
      Thanks Deepak!
      I added you to my google circles hope you don't mind

      Delete
    3. Cool.. Thanks for adding to google circles.

      Delete
  11. You have posted the blogs are really fantastic and informative.
    Mobile App Button

    ReplyDelete
  12. Excellent post!!! Java is most popular and efficient programming language available in the market today. It helps developers to create stunning desktop/web applications loaded with stunning functionalities. JAVA Training in Chennai | JAVA Training Institutes in Chennai

    ReplyDelete
  13. I known the lot of information and how it works then what are benefits by applying this application through this article.A great thanks for a valuable information.
    Android Training in chennai | Android Training chennai | Android course in chennai | Android course chennai

    ReplyDelete
  14. Deepak,

    Nice post! but how to click device back button using javascript?

    ReplyDelete
  15. Hi, I am Emi lives in Chennai. I am technology freak. I did Android mobile application development course in Chennai at reputed training institutes, this is very usful for me to make a bright carrer in IT industry. So If you looking for best Android Training Institutes in Chennai please visit fita academy. Android Training in Chennai



    ReplyDelete

  16. Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing.Nice article i was really impressed by seeing this article, it was very interesting and it is very useful for me.. Android Training in chennai | Android Training chennai | Android course in chennai | Android course chennai

    ReplyDelete
  17. Thanks for your informative article on ios mobile application development. Your article helped me to explore the future of mobile apps developers. Having sound knowledge on mobile application development will help you to float in mobile application development. iOS Training in Chennai

    ReplyDelete
  18. All are saying the same thing repeatedly, but in your blog I had a chance to get some useful and unique information, I love your writing style very much, I would like to suggest your blog in my dude circle, so keep on updates…
    Regards
    Angularjs training in chennai|Angularjs training chennai|Angularjs course in chennai

    ReplyDelete
  19. I really enjoy simply reading all of your weblogs. Simply wanted to inform you that you have people like me who appreciate your work. Definitely a great post. Hats off to you! The information that you have provided is very helpful.

    machine learning course

    artificial intelligence course in mumbai

    ReplyDelete
  20. 360DigiTMG is the best one in Hyderabad and is a Registered Education Provider (R.E.P.) by PMI to conduct training for this globally recognized certification.
    360DigiTMG pmp certification
    pmi acp certification

    ReplyDelete
  21. Thank you for sharing such a really admire your post. Your post is great!
    data science course in Hyderabad

    ReplyDelete
  22. The data that you provided in the blog is informative and effective.I am happy to visit and read useful articles here. I hope you continue to do the sharing through the post to the reader. Read more about


    selenium training in chennai

    selenium training in chennai

    selenium online training in chennai

    selenium training in bangalore

    selenium training in hyderabad

    selenium training in coimbatore

    selenium online training

    ReplyDelete
  23. Positive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work.
    DevOps Training in Chennai

    DevOps Online Training in Chennai

    DevOps Training in Bangalore

    DevOps Training in Hyderabad

    DevOps Training in Coimbatore

    DevOps Training

    DevOps Online Training

    ReplyDelete
  24. I known the lot of information and how it works then what are benefits by applying this application through this article.A great thanks for a valuable information.


    AWS Course in Bangalore

    AWS Course in Hyderabad

    AWS Course in Coimbatore

    AWS Course

    AWS Certification Course

    AWS Certification Training

    AWS Online Training

    AWS Training

    ReplyDelete
  25. Great post!I am actually getting ready to across this information,i am very happy to this commands.Also great blog here with all of the valuable information you have.Well done,its a great knowledge.

    IELTS Coaching in chennai

    German Classes in Chennai

    GRE Coaching Classes in Chennai

    TOEFL Coaching in Chennai

    spoken english classes in chennai | Communication training

    ReplyDelete
  26. Excellent post. I learned a lot from this blog and I suggest my friends to visit your blog to learn new concept about technology.Best data science courses in hyerabad

    ReplyDelete
  27. Thank you for providing this kind of useful information,I am searching for this kind of useful information; it is very useful to me and some other looking for it. It is very helpful to who are searching

    AI Training in Hyderabad

    ReplyDelete
  28. Very useful blog with unique content and excellent writing thank you.
    typeerror nonetype object is not subscriptable

    ReplyDelete
  29. Excellent blog information shared was very informative looking forward for next blog thank you.
    Data Analytics Course Online

    ReplyDelete
  30. Thanks for sharing this great information on Oracle ADF. Actually I was looking for the same information on internet for Oracle ADF Interview Questions and Answers/Tips and came across your blog. I am impressed by the information that you have on this blog. It shows how well you understand this subject, you can learn more aboutOracle ADF by attending Oracle ADF Training.

    ReplyDelete
  31. I curious more interest in some of them hope you will give more information on this topics in your next articles.
    data science training

    ReplyDelete
  32. If all the writers who pen down articles would give a thought to write topic specific articles like you, then more number of readers would read their content.
    SAP training in Mumbai
    SAP course in Mumbai

    ReplyDelete

  33. Very awesome!!! When I searched for this I found this website at the top of all blogs in search engines.
    Data Science Training in Hyderabad

    ReplyDelete
  34. Through this post, i do know that your smart information in fiddling with all the items was very useful. I advise that this can be the primary place wherever I notice problems i have been sorting out. you've got a creative nevertheless engaging approach of writing.
    data scientists training

    ReplyDelete
  35. Very nice job... Thanks for sharing this amazing and educative blog post!
    data science training in malaysia

    ReplyDelete
  36. Informative blog. Useful to many people. Thanks for sharing with us.
    Data Science Training Institute in Hyderabad

    ReplyDelete
  37. Interesting post. I Have Been wondering about this issue, so thanks for posting. Pretty cool post.It 's really very nice and Useful post.Thanks
    full stack web development course in malaysia

    ReplyDelete
  38. I really enjoy simply reading all of your weblogs. Simply wanted to inform you that you have people like me who appreciate your work. Definitely a great post. Hats off to you! The information that you have provided is very helpful. data analytics course in mysore

    ReplyDelete
  39. Любой желает узнать свое будущее и воспринимает конкретные варианты гадания более достоверными. Гадания на сердце на картах это способ просмотреть грядущие события непрерывно привлекал человека. Ворожение позволяет просмотреть, что человека ожидает в предстоящее время.

    ReplyDelete
  40. Just pure brilliance from you here. I have never expected something less than this from you and you have not disappointed me at all. I suppose you will keep the quality work going on.
    data science classes in hyderabad

    ReplyDelete
  41. Thank you for helping people get the information they need. Great stuff as usual. Keep up the great work!!!
    full stack developer course

    ReplyDelete
  42. I will truly value the essayist's decision for picking this magnificent article fitting to my matter.Here is a profound depiction about the article matter which helped me more.
    data science online training in hyderabad

    ReplyDelete
  43. Well stated, you have furnished the right information that will be useful to everybody. Thank you for sharing your thoughts. Security measures protect your company not only from data breaches, but also from excessive financial losses, a loss of people's trust, and potential risks to brand reputation and future benefits.
    SD Wan Service Providers SOC service providers
    SIEM solutions

    ReplyDelete
  44. Are you looking for a Data Analytics Course with placement assistance that can help you in landing a high-paying job then 360DigTMG is the right place for you. Enroll now


    Best Data Science Training institute in Bangalore

    ReplyDelete
  45. Get Data Science Certification from top-ranked universities UTM, Malaysia, and IBM. We provide extensive training for the future-ready workforce.
    data science training in malaysia

    ReplyDelete
  46. Latest Egg Rates in India With The Help Of Egg Rates Price

    ReplyDelete
  47. Address similar born newspaper agree arm. Push majority involve page forget.health

    ReplyDelete
  48. Occur form view safe second owner check. Kitchen thought audience blood do hard single.trending-updates

    ReplyDelete