lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I have made some progress and two files run almost as intented.
They run if I use
me:luaActivity('second')
instead of
me:intent_for_result(myIntent, callback)
but of course now my app.lua does not receive result from second.lua

How do I create myIntent? I tried with
myIntent = Content.Intent(me.a, SecondActivity.class)
but it does not work because SecondActivity.class is null.

Thanks
Fero Dali


File app.lua
local android = require 'android'
local SecondActivity = require'second'
local LPK = luajava.package
local Content = LPK 'android.content'

local RESULT_OK = -1

local MAIN = android.new()
function MAIN.create(me)
  local btn, txt
  local myIntent
  --- WHAT TO DO HERE?
  -- from java code
  --       Intent myIntent=new Intent(MainActivity.this,SecondActivity.class);
--  myIntent = Content.Intent(me.a, SecondActivity.class)

  local callback
  callback = function(request, result, intent)
    print(string.format('Callback  request: %d, result: %d', request, result))
    if result == RESULT_OK then
      local text = intent:getStringExtra('MESSAGE')
      txt:setText(text)
    end
  end

  btn = me:button('Start new Activity for result', function()
--                    me:intent_for_result(myIntent, callback)
                    me:luaActivity('second')
  end)
  txt = me:textView('Type some text in another activity')

  return me:vbox{
    btn,
    txt,
  }
end
return MAIN


File second.lua
local android = require 'android'
local LPK = luajava.package
local Content = LPK 'android.content'

local RESULT_OK = -1

local MAIN = android.new()
function MAIN.create(me)
  local edit, btn
  edit = me:editText()
  btn = me:button('Return from new Activity',
                  function()
                    local message = edit:getText():toString()
                    local myIntent = Content.Intent()
                    myIntent:putExtra('MESSAGE', message)
                    me.a:setResult(RESULT_OK, myIntent)
                    me.a:finish() -- finish activity
  end)
  return me:vbox{
    edit,
    btn,
  }
end
return MAIN