Search code examples
androidxamarin.androidstartactivityforresult

Unable to call activity for result?


Goal: Call method from another activity (dialog) and return result the user chooses

Note: Ive read as many articles as I can on going to other activities, as well as SO posts on this error. I have tried the solutions from about 8 of them and I am just stuck at this point.

Problem: I am getting the following error: enter image description here Ive looked at the manifest, but it doesnt have any of the projects activities so Im not sure why thats even a suggestion. The activity I am calling IS in the CSPROJ file. I'm also not sure what the HEX part is of the explicit activity class

I get the same error when I try it this way:

Intent intent;
        
if (item is Ph)
{ 
    intent = new Intent(this, typeof(CveActivity));
    intent.PutExtraMItem(item);
    StartActivityForResult(intent, PH_REQUEST_CODE);
}

What Ive tried:

  • StartActivity
  • StartActivityForResult
  • Calling the method of that activity
  • Instantiating an object and calling the method of that activity/class
  • Checked that this does exist in the csproj file

Solution

  • Based on the error log, you can try to add Activity tag to your activities(e.g. CveActivity).

        [Activity(Label = "CveActivity")] 
        public class CveActivity : Activity{
    
         }
    

    I created a demo and achieved this function, you can refer to the following code:

    MainActivity.cs

    [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)] 
    public class MainActivity : AppCompatActivity
    {
        Button button;
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.activity_main);
    
            button = FindViewById<Button>(Resource.Id.button1);
            button.Click += Button_Click;
    
        }
    
        private void Button_Click(object sender, System.EventArgs e)
        {
            Intent intent = new Intent(this, typeof(SecondActivity));
    
            StartActivityForResult(intent, 100);
        }
    
        protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
        {
            base.OnActivityResult(requestCode, resultCode, data);
    
            if (requestCode == 100) {
                if (resultCode == Result.Ok)
                {
                    Toast.MakeText(this,"result is: " + data.GetStringExtra("Test"),ToastLength.Long).Show();
                }
            }
        }
    }
    

    activity_main.xml

    <?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <Button
            android:id="@+id/button1"
            android:text="test"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </LinearLayout>
    

    SecondActivity.cs

    [Activity(Label = "SecondActivity")] 
    public class SecondActivity : Activity
    {
        Button button;
    
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
    
            // Create your application here
            SetContentView(Resource.Layout.second_layout);
    
            button = FindViewById<Button>(Resource.Id.button1);
            button.Click += Button_Click; ;
        }
    
        private void Button_Click(object sender, EventArgs e)
        {
            var intent = new Intent();
            intent.PutExtra("Test","test");
            SetResult(Result.Ok,intent);
            Finish();
        }
    }
    

    second_layout.xml

    <?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
            <Button
            android:id="@+id/button1"
            android:text="return data"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    
    </LinearLayout>