简体   繁体   中英

Navigate back to calling Fragment from another activity

I have a MainActivity which has 3 fragments. 2 of these fragments have lists in them and on click of a list item I go to another activity (ie. NewActivity). I have an ActionBar implemented on the NewActivity and want to go back to the calling fragment from this activity.

I have the following code:

MainActivity.cs

SupportActionBar.SetDisplayHomeAsUpEnabled(true);
            SupportActionBar.SetDisplayShowHomeEnabled(true);
var ft = SupportFragmentManager.BeginTransaction();
            ft.Add(Resource.Id.fragment_container, homeFragment, "FRAG_HOME").SetBreadCrumbShortTitle("FRAG_HOME");

            ft.Add(Resource.Id.fragment_container, FragmentList1, "FRAG_LIST1").SetBreadCrumbShortTitle("FRAG_LIST1");

            ft.Hide(FragmentList1);
            ft.Add(Resource.Id.fragment_container, FragmentList2, "FRAG_LIST2").SetBreadCrumbShortTitle("FRAG_LIST2");
            ft.AddToBackStack(null);
            ft.Hide(FragmentList2);

            ft.Commit();

FragmentList1.cs

 void LvLatestArticles_ItemClick (object sender, AdapterView.ItemClickEventArgs e)
        {
            Intent intent = new Intent(Activity, typeof(NewActivity));
            StartActivity(intent);
        }

NewActivity.cs

public override bool OnOptionsItemSelected(IMenuItem item)
        {
            switch(item.ItemId)
            {
                case Android.Resource.Id.Home:
                    SupportFragmentManager.PopBackStackImmediate();
                    break;
            }
            return base.OnOptionsItemSelected(item);
        }

In the above code, I override the Home button functionality on ActionBar. But it simply does not do anything. How do I go forward with this? Any help would be appreciated

Try calling OnBackPressed() on the Home button . Something like so :-

    public override bool OnOptionsItemSelected(IMenuItem item){
         switch(item.ItemId){
            case Android.Resource.Id.Home:
                     base.OnBackPressed();                    
                     break;
            }
         return base.OnOptionsItemSelected(item);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM