Intent.GetStringExtra returns null value

KA 76 Reputation points
2022-05-27T16:04:29.483+00:00

Hello,
I am trying to pass the text of an EditText object from one screen (MainActivity.cs) in Xamarin Forms as the Text of another EditText in a second screen (details_page.cs).
However, on the second screen when I am trying to get the value of Intent.GetExtraString() it is always coming up as null.

Any idea what may be wrong here:

MainActivity.cs

using System;
using Android.App;
using Android.OS;
using Android.Runtime;
using Android.Views;
using AndroidX.AppCompat.App;
using Android.Widget;
using Android.Content;


namespace hikmatNotes
{
    [Activity(Label = "@string/app_name", Theme = "@style/AppTheme.NoActionBar", MainLauncher = true)]
    public class MainActivity : AppCompatActivity
    {
        EditText inputNotes;
        Button btnProcess;

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            SetContentView(Resource.Layout.activity_main);

            inputNotes = FindViewById<EditText>(Resource.Id.inputNotes);
            btnProcess = FindViewById<Button>(Resource.Id.btnProcess);
           //   btnProcess.Click += btnProcess_click;
            btnProcess.Click += delegate
            {
                var myIntent =  new Intent(this, typeof(details_page));
                myIntent.PutExtra("InputNotes", inputNotes.Text.ToString());
                StartActivity(typeof(details_page));
            };

details_page.cs

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace hikmatNotes
{
    [Activity(Label = "Activity1")]
    public class details_page : Activity
    {
        EditText outputNotes;
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            SetContentView(Resource.Layout.details_page);

            outputNotes = FindViewById<EditText>(Resource.Id.outputtNotes);
            string inputText = Intent.GetStringExtra("InputNotes");

            outputNotes.Text = inputText;

        }
    }
}
Developer technologies | .NET | Xamarin
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. KA 76 Reputation points
    2022-05-27T16:31:05.203+00:00

    I got it.

    The error was in line 32 of the MainActivity.cs file.

    It should be:

    StartActivity(typeof(myIntent)); //earlier it was wrongly doing this for details_page
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.