lua-users home
lua-l archive

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


If this is not appropiate place to ask this kind of questions please
tell me where to ask them.

I am using AndroLua from Steve Donovan.
I am trying to implement app like this:
https://www.javatpoint.com/android-startactivityforresult-example
It is activity that calls another activity to get result from it.
I made two files app.lua and second.lua included in the end of this
email. Since I have not found any exaple on the internet how to use
me:intent_for_result(myIntent, callback)
function I am asking here for help.
For one I guess I need to "require'second'" in my app.lua file: where and how?
And more important: how do I use 'android.content.Intent' in both files?
Can anyone help me finish these two files to working exaple?
I have marked with comments
--- WHAT TO DO HERE? 
places where I do not know what to place.

Thanks a lot
Fero Dali

My app.lua file:
local android = require 'android'

local LPK = luajava.package
local Intent = LPK 'android.content.Intent'

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);

  local callback
  callback = function(request, result, intent)
    print(string.format('Callback  request: %s, result: %s, intent: %s', request, result, intent))
    local text = intent:getStringData('MESSAGE')
    txt:setText(text)
  end

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

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


My second.lua file:
local android = require 'android'

local LPK = luajava.package
local Intent = LPK 'android.content.Intent'

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()
                    local myIntent
                    myIntent = Intent.Intent()
                    myIntent.putExtra('MESSAGE', message)
                    --- WHAT TO DO HERE?
                    -- setResult(myIntent, 2)
                    -- finish() -- finish activity
  end)
  return me:vbox{
    edit,
    btn,
  }
end
return MAIN