0

In my Requirement having wizard step.So If I am in Second Step.And pressing back.And Now I m in first wizard.And pressing next so it recreate activity.that's why I have put it into OnCreate.

When I go to that activity for first time then My Adapter notifydatasetchange work properly but when I'm go back and again goes to same activity Notifydatasetchange() not working means onbindviewholder not called.

Even I have Tried to set adapter again but it's not still called.This is my code.What I have missing? Any help is appreciated!

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.ProposalInviteOwnersView);
       _lstviewNearByPeople = FindViewById<RecyclerView>(Resource.Id.lstviewNearByPeople);
        _layoutmanager = new LinearLayoutManager(this, LinearLayoutManager.Horizontal, false);
        _lstviewNearByPeople.HasFixedSize = true;
        _lstviewNearByPeople.SetLayoutManager(_layoutmanager);
        BindtoNearByAdapter();
    }

    public void BindtoNearByAdapter()
    {
        try
        {
            if (GlobalConst.LstNearByPeople.Count > 0)
            {
                _lstNearByPeople = new List<Owners>();
                _lstNearByPeople.AddRange(GlobalConst.LstNearByPeople);

                if(_nearByCollaboratorAdapter==null)
                {
                    _lstviewNearByPeople.Visibility = ViewStates.Visible;

                    _nearByCollaboratorAdapter = new NearByCollaboratorAdapter(this, _lstNearByPeople, this, mlastPosition);
                    _lstviewNearByPeople.SetAdapter(_nearByCollaboratorAdapter);

                }
                else
                {
                    _nearByCollaboratorAdapter.updateItems(_lstNearByPeople);

                }

            }
            else
            {
                _noNearByDevicelayout.Visibility = ViewStates.Visible;
                _lstviewNearByPeople.Visibility = ViewStates.Gone;
            }


        }
        catch (System.Exception ex)
        {
            Core.Helpers.GlobalLogic.createCrashReport(ex);
            Toast.MakeText(this, ex.Message.ToString(), ToastLength.Short).Show();
        }
    }

    public class NearByCollaboratorAdapter : RecyclerView.Adapter
{
    public List<Owners> _lstNearByPeople;
    readonly ISelectCollaborator _listner;
    readonly Activity _context;
    protected int mLastPosition;


    public NearByCollaboratorAdapter(Activity context, List<Owners> lstNearByPeople, ISelectCollaborator listner, int _mLastPosition)
    {
        _lstNearByPeople = lstNearByPeople;
        _context = context;
        _listner = listner;
        mLastPosition = _mLastPosition;

    }
    public override int ItemCount => _lstNearByPeople.Count;

    public void updateItems(List<Owners> lstNearByPeople)
    {
        _lstNearByPeople.Clear();
        _lstNearByPeople.AddRange(lstNearByPeople);
        this.NotifyDataSetChanged();
    }






}
Vanjara Sweta
  • 81
  • 1
  • 10
  • 1
    Call your adapter BindtoNearByAdapter() method in onResume() as it will get called when you come back to your activity. Also check the lifecycle events of activity. – Yyy May 31 '19 at 12:59
  • I have tried also to call on onResume(). But I again come this activity is called onCreate().So may be no issue. – Vanjara Sweta May 31 '19 at 13:04
  • Try to remove your if condition which checks GlobalConst.LstNearByPeople.Count. Or try to debug on that point, it might be going to else part. – Yyy May 31 '19 at 13:06
  • No it's not going to else part. – Vanjara Sweta May 31 '19 at 13:22

3 Answers3

2

Implement an onResume method...

@Override
public void OnResume (){
  super.onResume();
  BindtoNearByAdapter()
}
COYG
  • 1,538
  • 1
  • 16
  • 31
  • I have already called on OnCreate() so why I have called on OnResume () – Vanjara Sweta May 31 '19 at 12:53
  • On create is only called when the process is killed and you are recreating it. When it you proceed to another activity from that activity it is still in the stack. So when you go back it doesn't need to be created again... see here https://stackoverflow.com/questions/15658687/how-to-use-onresume – COYG May 31 '19 at 13:07
  • Yes,I know.But in my Requirement having wizard step.So If I am in Second Step.And pressing back.And Now I m in first wizard.And pressing next so it recreate activity.that's why I have put it into OnCreate. – Vanjara Sweta May 31 '19 at 13:22
  • @VanjaraSweta have it both in onCreate and OnResume – COYG May 31 '19 at 14:52
1

OnCreate is called only once, when you first launch the activity. If you move to next activity and come back to older activity, OnCreate is not called again. So you need to shift your BindtoNearByAdapter() to OnStart or OnResume as posted by @COYG. I will recommend OnStart.

You can also check this link for better life-cycle understanding link

Anand Kumar
  • 1,439
  • 1
  • 15
  • 22
  • Yes,I know.But in my Requirement having wizard step.So If I am in Second Step.And pressing back.And Now I m in first wizard.And pressing next so it recreate activity.that's why I have put it into OnCreate. – Vanjara Sweta May 31 '19 at 13:22
1

I have created a simple demo to simulate your code, but it worked properly. The main code is as follows: PhotoAlbumAdapter.cs

 public class PhotoAlbumAdapter: RecyclerView.Adapter
{
    public event EventHandler<int> ItemClick;

    public List<Photo> mPhotoAlbum;

    //readonly ISelectCollaborator _listner;
    readonly Activity _context;
    protected int mLastPosition;

    public PhotoAlbumAdapter(Activity context, List<Photo> _mPhotoAlbum)
    {
        mPhotoAlbum = _mPhotoAlbum;
        _context = context;
        //mLastPosition = _mLastPosition;
    }

    public override RecyclerView.ViewHolder
        OnCreateViewHolder(ViewGroup parent, int viewType)
    {
        View itemView = LayoutInflater.From(parent.Context).
                    Inflate(Resource.Layout.PhotoCardView, parent, false);

        PhotoViewHolder vh = new PhotoViewHolder(itemView, OnClick);
        return vh;
    }

    // Fill in the contents of the photo card (invoked by the layout manager):
    public override void
        OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
    {
        PhotoViewHolder vh = holder as PhotoViewHolder;

        vh.Image.SetImageResource(mPhotoAlbum[position].PhotoID);
        vh.Caption.Text = mPhotoAlbum[position].Caption;
    }

    public override int ItemCount
    {
        //get { return mPhotoAlbum.Capacity; }
        get { return mPhotoAlbum.Count; }
    }

    void OnClick(int position)
    {
        if (ItemClick != null)
            ItemClick(this, position);
    }

    public void updateItems(List<Photo> lstNearByPeople)
    {
        mPhotoAlbum.Clear();
        mPhotoAlbum.AddRange(lstNearByPeople);
        this.NotifyDataSetChanged();
    }
}


public class PhotoViewHolder : RecyclerView.ViewHolder
{
    public ImageView Image { get; private set; }
    public TextView Caption { get; private set; }

    public PhotoViewHolder(View itemView, Action<int> listener)
        : base(itemView)
    {
        Image = itemView.FindViewById<ImageView>(Resource.Id.imageView);
        Caption = itemView.FindViewById<TextView>(Resource.Id.textView);

        itemView.Click += (sender, e) => listener(base.LayoutPosition);
    }
}

MainActivity.cs

      private void BindtoNearByAdapter()
    {
        if (GlobalConst.mBuiltInPhotos.Length > 0)
        {
            _lstNearByPeople = new List<Photo>();
            _lstNearByPeople.AddRange(GlobalConst.mBuiltInPhotos);

            if (mAdapter == null)//once press back button, the activity is destroyed ,so the code go here every time
            {
                mAdapter = new PhotoAlbumAdapter(this, _lstNearByPeople);
                mRecyclerView.SetAdapter(mAdapter);
            }
            else  
            {// code never come here
                //mAdapter.updateItems(_lstNearByPeople);
            }

        }
        else
        { //if the data is not empty,  code never come here
            //_noNearByDevicelayout.Visibility = ViewStates.Visible;
            //_lstviewNearByPeople.Visibility = ViewStates.Gone;
        }
    }

The effect is as follows: enter image description here

Note: Since I couldn't see the full code of your class NearByCollaboratorAdapter and the GlobalConst.LstNearByPeople,so you can check and debug these code of your app.

Jessie Zhang -MSFT
  • 9,830
  • 1
  • 7
  • 19
  • 1
    Okay,Thanks. I have resolved issue.There is no problem in my code.Actually propblem in that I have used P2Pkit library for Near by peers detect.So my context was changed. So finally I have done :) – Vanjara Sweta Jun 03 '19 at 12:32