0

I am trying to send the string of data from my Activity.cs to Fragment1.cs and display that in my Fragment1.xml layout . If you see in my Activity.cs I want to send "text" string to my Fragment.cs.

Note: var text = newSentence.ToString(); //this text i want to send to my Fragmnet.cs

Please help me thanks in advance

Activiy.cs

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
        {
            if ((requestCode == PickImageId) && (resultCode == Result.Ok) && (data != null))
            {
                //Android.Net.Uri uri = data.Data;
                var path = UriHelper.GetPathFromUri(this, data.Data);
                // var path = GetPathToImage(uri);
                //System.Diagnostics.Debug.WriteLine("Image path == " + path);
                // Bitmap bitmap = BitmapFactory.DecodeFile(path);
                Bitmap bitmap =  loadAndResizeBitmap.Conversionandresize(path,4032,3024);
                ColorMatrix matrix = new ColorMatrix();
                matrix.SetSaturation(0);
               _imageView.SetColorFilter(new ColorMatrixColorFilter(matrix));
               _imageView.SetImageBitmap(bitmap);

                TextRecognizer txtRecognizer = new TextRecognizer.Builder(ApplicationContext).Build();
                if (!txtRecognizer.IsOperational)
                {
                    Log.Error("Error", "Detector dependencies are not yet available");
                }
                else
                {
                    Frame frame = new Frame.Builder().SetBitmap(bitmap).Build();
                    SparseArray items = txtRecognizer.Detect(frame);
                    StringBuilder strBuilder = new StringBuilder();
                    for (int i = 0; i < items.Size(); i++)
                    {
                        TextBlock item = (TextBlock)items.ValueAt(i);
                        strBuilder.Append(item.Value);
                        strBuilder.Append("\n");

                    }

                    int myLimit = 10;
                    string sentence = strBuilder.ToString();
                    // string[] words = sentence.Split("/n", ',');
                    string[] words = sentence.Split(new[] { " ","\n\r"}, StringSplitOptions.RemoveEmptyEntries);

                    StringBuilder newSentence = new StringBuilder();
                    string line = " ";
                    foreach (string word in words)
                    {
                        if ((line + word).Length > myLimit)
                        {
                            newSentence.Append(line);
                            line = " ";
                        }

                        line += string.Format("{0} ", word);
                    }

                    if (line.Length > 0)
                        newSentence.Append(line);

                    var text = newSentence.ToString();
}

Fragment1.cs

namespace OCR_Pro
{
    public class Fragment1 : Android.Support.V4.App.Fragment
    {
        public override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
        }

        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {

            var view = inflater.Inflate(Resource.Layout.Fragment1, container, false);
            var textView = view.FindViewById<TextView>(Resource.Id.txtView);

            return view;
        }
    }
}
  • Possible duplicate of [Xamarin Android passing variable from Activity to Fragment returns null](https://stackoverflow.com/questions/26509072/xamarin-android-passing-variable-from-activity-to-fragment-returns-null) – diogenesgg Sep 24 '19 at 16:57
  • @kiran, please see the same article:https://stackoverflow.com/questions/58086556/isharedpreferences-stored-string-is-updating-after-restart-of-my-app/58092305#58092305 – Cherry Bu - MSFT Sep 25 '19 at 06:30

0 Answers0