Forum Xamarin.Android

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

Activity not showing in the "Open Applications"

meirrotfleischmeirrotfleisch USMember ✭✭✭
edited June 2020 in Xamarin.Android

Hi

I have an activity (not main launcher) that when I click the "android hardware back button" it closes the app instead of leaving it in background.

how can I force it to remain in memory in the list of "open applications"

regards

M.R.

Best Answer

Answers

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    Based on your description, do you want to achieve the result like following GIF?

    If so, you can override OnKeyDown method. re-write the back button function, achieve function is same as home button.

      [Activity(Label = "Activity1")]
        public class Activity1 : Activity
        {
            protected override void OnCreate(Bundle savedInstanceState)
            {
                base.OnCreate(savedInstanceState);
                SetContentView(Resource.Layout.layout1);
                // Create your application here
            }
    
            public override bool OnKeyDown([GeneratedEnum] Keycode keyCode, KeyEvent e)
            {
                if (keyCode== Keycode.Back)
                {
                    Intent intent = new Intent(Intent.ActionMain);
                    intent.SetFlags(ActivityFlags.NewTask);
                    intent.AddCategory(Intent.CategoryHome);
                    StartActivity(intent);
    
                    return true;
    
                }
                return base.OnKeyDown(keyCode, e);
    
            }
        }
    }
    
  • meirrotfleischmeirrotfleisch USMember ✭✭✭

    @LeonLu

    Thanks for your response.. Unfortunately this is still not working.

    In debug in visual studio I see that keycode.Back code is being fired but when I then go to the "open applications" the activity is not showing

    Was I supposed to someplace in the code enter my activity name?

    Regards

    M.R.

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    but when I then go to the "open applications" the activity is not showing

    Can you share your code about "open applications"?

  • meirrotfleischmeirrotfleisch USMember ✭✭✭
    Hi attached the screenshot. This is gotten by pressing the left button 🔘 on my samsung S5 device

    Hope 🙏 this helps
  • LeonLuLeonLu Member, Xamarin Team Xamurai

    Can you test his code in other device? This issue may be related to the samsung custom android OS.

  • meirrotfleischmeirrotfleisch USMember ✭✭✭

    @LeonLu

    Hi,
    Im a samsung guy! all my devices are samsung. I do have a lenovo Tablet but its old Android 4.4 .Xamarin wont run on it!! :(

    Any other suggestions?

    M.R.

Sign In or Register to comment.