0

I am using intent to pass my data from one activity(auth page) to another activity to have access to the Facebook loggedIn user. I am not seeing any error in my code but the text to display the username returns empty(blank). What is missing in here? I followed answers of sample issues on S.O to achieve this.

auth.cs

mProfileTracker = new MyProfileTracker();
            mProfileTracker.mOnProfileChanged +=  FindFaceBookUserProfile;
            mProfileTracker.StartTracking();

    private void FindFaceBookUserProfile(object sender, OnProfileChangeEventArgs e)
            {
                        LoggedInUserName.Text = position.FindProfile.Name;
                        LoggedInUserLastName.Text = position.FindProfile.Name;
        MajorActivity));
                        intent.PutExtra(MajorActivity.EXTRA_NAME, LoggedInUserName.Text);
                        this.StartActivity(intent);

                    }

public void OnSuccess(Java.Lang.Object result)
        {
            LoginResult loginResult = result as LoginResult;
            Console.WriteLine(loginResult.AccessToken.UserId);
             GetId= loginResult.AccessToken.UserId;
        }
 protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)

        {
            base.OnActivityResult(requestCode, resultCode, data);
            mCallbackManager.OnActivityResult(requestCode, (int)resultCode, data );
        }

MajorActivity

        public const string EXTRA_NAME = "UserName";
        protected override void OnCreate(Bundle savedInstanceState)
        {

            base.OnCreate(savedInstanceState);

            string UserName = Intent.GetStringExtra(EXTRA_NAME);
}

var headerView = navigationView.GetHeaderView(0);
GetUserName= headerView.FindViewById<TextView>(Resource.Id.loginUser);
GetUserName.Text = UserName;
  • please post your code for LoggedInUserName as well – Red M Jun 16 '17 at 16:44
  • @RedM, i am able to authenticate my users. When i output LoggedInUserName in auth.cs, i am able to see the name. But i am trying to pass the data to MajorActivity's navigation view. The problems seems to come from the intent part.. I am sure :) Code updated –  Jun 16 '17 at 16:54

2 Answers2

0

You are trying to extract string in your intent. You need to put a string as well I believe. try

intent.PutExtra(MajorActivity.EXTRA_NAME, LoggedInUserName.toString());

Reference Android Intent.getStringExtra() returns null

wick.ed
  • 473
  • 7
  • 15
  • `intent.PutExtra(MajorActivity.EXTRA_NAME, LoggedInUserName.Text.toString())`; this is what i have now.. it still blank. I don't get an error running the app. App works fine but text is blank. Please note i did some little changes to my code above. That is LoggedInUserName.Text –  Jun 16 '17 at 17:09
  • This part should be inside the onCreate method. Because your UserName String is local to that method. var headerView = navigationView.GetHeaderView(0); GetUserName= headerView.FindViewById(Resource.Id.loginUser); GetUserName.Text = UserName; – wick.ed Jun 16 '17 at 19:01
  • It is already in my onCreate Method :) but when i set GetUserName.Text ="Hello" for instance, it works. Could it be that, the UserName is not reaching or something? –  Jun 16 '17 at 19:31
  • ok do this. Make String USername a global variable and try. If that doesn't work put a breakpoint there and run it in debug mode and check what value string UserName gets when you evaluate that expression at that line string UserName = Intent.GetStringExtra(EXTRA_NAME); – wick.ed Jun 16 '17 at 20:05
  • i used a global variable for Username now. It is not working and i just consoled the Username. Looking at my log cat, nothing is pushed from the auth.cs. That is why it is blank. The baseActivity runs before the auth i am quite sure. please note: MajorActivity is a baseClass and auth.cs is mapped to be MajorActivity like (Auth: MajorActivity) –  Jun 16 '17 at 20:12
  • LoggedInUserName.Text is not empty in your auth.cs class right ? you checked it – wick.ed Jun 16 '17 at 20:13
  • no it not empty.. I am able to see the name in the auth.cs –  Jun 16 '17 at 20:14
  • replace `intent.PutExtra(MajorActivity.EXTRA_NAME, LoggedInUserName.Text);` with `intent.PutExtra(MajorActivity.EXTRA_NAME, LoggedInUserName.Text.toString());`. Also, check in console values of `MajorActivity.EXTRA` and `LoggedInUserName.Text.toString()` right after this line ` – wick.ed Jun 16 '17 at 20:18
  • I have been running series of test.After the consoling the values, i see the all the necessary data i want to pass. So the problem now is from the Major activity. How to get the data been passed because i just did a console test in the Major class and i am not receiving the data –  Jun 16 '17 at 20:55
0

In your auth.cs, I do not see you allocation new memory for your intent, so try this:

Intent i = new Intent(this, MajorActivity.class);
i.putExtra(MajorActivity.EXTRA_NAME, LoggedInUserName.Text);
startActivity(intent);

Then, in your MajorActivity.class, do this:

Bundle extras = getIntent().getExtras();
if(extras !=null) {
    String value = extras.getString(MajorActivity.EXTRA_NAME);
}
Red M
  • 2,609
  • 3
  • 30
  • 50
  • I am actually using Xamarin.Droid. (C#). Looks like your more of Java –  Jun 16 '17 at 17:17
  • yeah, I'm more Java. Can you translate this logic into your code though? – Red M Jun 16 '17 at 17:18
  • I would have look into the Xamarin forums but i get the logic –  Jun 16 '17 at 17:21
  • Looking at Xamarin, it is done just the way i have mine. I have actually used this in one of my activities and it worked fine. I can't really tell why my code wouldn't work –  Jun 16 '17 at 17:23