Android Question Starting Activity with an Intent

Haris Hafeez

Active Member
Licensed User
Longtime User
Hi All,

So I've got a couple of Activities.

Activity A - Allows users to create and saved messages
Activity B - Lists all the messages with the ability to edit each stored message.

I want the edit button on each of the items shown on Activity B to launch Activity A and pass the stored message.

I have the following code:

Manifest: (NewScheduledMessageActivity is Activity A for reference)
B4X:
AddActivityText(NewScheduledMessageActivity, <intent-filter>
  <action android:name="com.zarhouni.messagingpa.EDIT_MESSAGE" />
  <category android:name="android.intent.category.DEFAULT" />
</intent-filter>)

In Activity B:
B4X:
Sub EditButton_Click
    Dim btn As Button = Sender
    Dim EditIntent As Intent
    EditIntent.Initialize("com.zarhouni.messagingpa.EDIT_MESSAGE","")
    EditIntent.AddCategory("android.intent.category.DEFAULT")
    EditIntent.PutExtra("PendingMessage",PendingMessage)
    StartActivity(EditIntent)
End Sub

In Activity A:
B4X:
Sub CopyExistingValuesToFields(InitIntent As Intent)
    Dim SavedMessage As PendingMessage = InitIntent.GetExtra("PendingMessage")
    LblDate.Text = SavedMessage.Date
    LblTime.Text = SavedMessage.Time
    TxtMessage.Text = SavedMessage.StrMessage
    TxtTo.Text = SavedMessage.StrTo
End Sub
Sub Activity_Resume
    Dim InitIntent As Intent = Activity.GetStartingIntent
    If InitIntent <> Null Then
        If InitIntent.HasExtra("PendingMessage") Then
            CopyExistingValuesToFields(InitIntent)
        End If
    End If
End Sub

I can see while debugging that the Intent received by Activity A has no extras. In fact, all the member fields are null while only the Action URI is correctly set to com.zarhouni.messagingpa.EDIT_MESSAGE.

The InitIntent.HasExtra("PendingMessage") check doesn't evaluate to true.

What is the small thing I am missing here?

Thanks!
 

LucaMs

Expert
Licensed User
Longtime User
You don't need that all work

Create a sub in ActivityA that can receive the messag, for example:
Public Sub ReceiveMsg(Msg As String);

then, from ActivityB, when a message is selected, use:
CallSubDelayed2(ActivityA, ReceiveMsg, SelectedMsg)
 
Upvote 0

Haris Hafeez

Active Member
Licensed User
Longtime User
You don't need that all work

Create a sub in ActivityA that can receive the messag, for example:
Public Sub ReceiveMsg(Msg As String);

then, from ActivityB, when a message is selected, use:
CallSubDelayed2(ActivityA, ReceiveMsg, SelectedMsg)
Thanks Lucas. I know Intent is a bit of an overkill for this simple example, but I'm learning and wanted to learn how to use Intents to launch an Activity and pass information to it.
But can you elaborate how I will launch/display the Activity A in your case?
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User


CallSubDelayed (and its variants) launches the Activity called (A) after the current routine (ActivityB) terminates or adding Activity.Finish after the call statement.
 
Upvote 0

Haris Hafeez

Active Member
Licensed User
Longtime User
CallSubDelayed (and its variants) launches the Activity called (A) after the current routine (ActivityB) terminates or adding Activity.Finish after the call statement.
Thanks Lucas.
Any idea about why the Intent mechanism is not working for me?

Cheers.
 
Upvote 0

Haris Hafeez

Active Member
Licensed User
Longtime User
Anyone please? I would really appreciate to know how intents work in B4A. I seem to be missing a small bit somewhere but can't quite figure out what it is
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I remember (strange ) my first steps with B4A.

I was searching for StartActivityForResult, for this topic; then, I spent days and I understood that what I have stated is the easiest and best.

However, you can search on the site, there are probably many posts about this.

I do not use them; maybe I can help you informing me better, but not right now, sorry.
 
Upvote 0

Haris Hafeez

Active Member
Licensed User
Longtime User
PendingMessage is a custom type declared as.
B4X:
    Type PendingMessage (StrTo As String, StrMessage As String, Date As String, Time As String)

This gets stored and retrieved using the KeyValueStore.

[EDIT] Although there is a custome type in my program but I am not passing the custom type as extra to the intent. I am passing a String.

Luca, seems I I have started off in your footsteps I will be happy if I can earn an Expert badge like yourself after a bit of hard work
 
Upvote 0

Haris Hafeez

Active Member
Licensed User
Longtime User
TDS: Mant thanks. Your mention of serializable forced me to look at the code. I was indeed storing the custom type instead of a String(which is what I wanted to do). So I changed the code to send a String in the intent extras and that worked.

That makes me wonder though. Can we not pass any object via Intent.PutExtra(strKey, object) ?
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…