Titanium JIRA Archive
Appcelerator Community (AC)

[AC-3592] Android Picker not retaining selected value

GitHub Issuen/a
TypeBug
Priorityn/a
StatusClosed
ResolutionWon't Fix
Resolution Date2016-05-05T18:59:30.000+0000
Affected Version/sAppcelerator Studio 4.4.2
Fix Version/sn/a
ComponentsAlloy, Titanium SDK & CLI
Labelsandroid
ReporterSoumya Kanti Kar
AssigneeShak Hossain
Created2016-05-03T21:41:45.000+0000
Updated2016-05-05T18:59:30.000+0000

Description

Consider the following code below:
function doClick(e) {
    win.open();
}

var picker = null;
var backBtn = null;
var selectedIdx = 0;

$.index.open();

var win = Titanium.UI.createWindow({
    exitOnClose : false,
    navBarHidden : true
});

win.addEventListener("open", function() {

    if(null != picker) {
        //picker.setSelectedRow(0, selectedIdx, false);
        return;
    }

    picker = Titanium.UI.createPicker({
        width : "75%",
        height : "50dip",
        selectionIndicator : true
    });

    picker.addEventListener("change", function(id) {
        selectedIdx = id.rowIndex;
    });

    var arr = [];
    for(var i = 0; i < 10; i++) {
        arr.push(Titanium.UI.createPickerRow({
            title : i,
            color : "black"
        }));
    }

    selectedIdx = 0;

    picker.add(arr);

    backBtn = Titanium.UI.createButton({
        bottom : "10dip",
        title : "Close"
    });
    backBtn.addEventListener("click", function() {
        win.close();
    });

    win.add(picker);
    win.add(backBtn);
});
If the above code is executed with SDK 5.2.0.GA or 5.2.2.GA, then you will see that the picker value is not being retained. After ever open/close of the window, even if we reuse the old picker object. But, if you uncomment the setSelectedRow (hack code I would say), then it behaves as required. Anyone faced such issue or is aware of a proper solution or root cause for it.

Comments

  1. Nazmus Salahin 2016-05-04

    Hello, I am able to reproduce this issue with given code and given step. But to my understanding this is expected behavior. The picker is attached to a window and on Android starting with Release 3.2.0, all the windows are heavyweight. A heavyweight window is associated with a new Android Activity. So when the window is closed the activity is closed and so will be the picker. It that case the picker is not supposed retain its selected value. Thanks *Environment*: *Device info:* Nexus7 (android 6.0.1) *Node.js Version:* 0.12.7 *npm Version:* 2.11.3 *Titanium SDKs:* 5.2.2.GA and 5.2.1.GA *Java Development Kit Version:* 1.8.0_73 *Titanium CLI Version:* 5.0.5 *Appcelerator CLI Version:* 5.2.2 *Appcelerator Studio:* 4.5.0
  2. Soumya Kanti Kar 2016-05-04

    But shouldn't the picker retain the last selected value? I am not creating a new object of the picker, I am just closing/opening the window. Also, if I un-comment the commented line, then it works, meaning that the internal state of the picker is being reset to the default value after closing and opening the window. We need to manually restore the last selected value to make it work properly. Also if we add a textfield along with the picker, then the textfield retains its value, during the opening/closing of the window.
  3. Sharif AbuDarda 2016-05-04

    Hello, You have to set the setSelectedRow method after picker has been displayed for automatically select a row in each column. For reference see the example codes http://docs.appcelerator.com/platform/latest/#!/api/Titanium.UI.Picker-method-setSelectedRow. So, according to your code, the commented code must be at the end after you add picker to window in your window open function. Regards, Sharif.
  4. Soumya Kanti Kar 2016-05-04

    If I move the commented code at the end, after adding the picker to the window, then it will work for the first time. After I close the window and open it again, the last selected value is not being retained. For example, if I set the selectedIdx as 5, then it will work properly. Now I change it to 8 and close the window. On opening the window again, it will show 5, instead of 8, the last selected value of 8.
  5. Sharif AbuDarda 2016-05-05

    Hello, Remove the "selectedIdx = 0;" in the open function, and use the "picker.setSelectedRow(0, selectedIdx, false);". A heavyweight window is associated with a new Android Activity. So when the window is closed the activity is closed and so will be the picker. It that case the picker is not supposed retain its selected value. This should be the default behavior. If you want to retain the picker selected value you have to use the commented line of code. Closing this issue. Regards, Sharif.

JSON Source