0

在我的应用程序中,用户需要登录才能访问该应用程序。我的问题是当用户从主要活动中按下返回键时,用户被发送回登录屏幕。

我知道我可以覆盖 ht OnBackPressed() 但这并不能解决我的问题。如果我覆盖 OnBackPressed() 并将用户发送到主屏幕,当用户再次打开应用程序时,用户将返回登录屏幕。即使用户已经显示登录屏幕,用户在后台登录。所以为了进入主要活动,用户需要再次登录。

我试过 Finish() 但单声道机器人显示错误。

我已附上我的登录活动代码。有人可能会让我摆脱这个问题。

using System;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using RestSharp;
using TheNorthStar.Api.Requests;
using TheNorthStar.Api.Results;
using NorthStar.Driver.Application;
using Android.Preferences;
using Object = Java.Lang.Object;


namespace NorthStar.Driver
{
    public class DriverLogonAsync : AsyncTask

    {
        private ProgressDialog processDialog;
        private Context m_context;
        private DriverLogon m_driver;

        private bool _resterror;

        public DriverLogonAsync( Context context, DriverLogon driver )
        {
            m_context = context;
            m_driver = driver;

            _resterror = false;
        }

        /*
         * throws
         * should separate out logic and use MyMessagebox..
         */
        private void SetComfirmAlertBox(string carNum, DriverLogonResult result)
        {
            var api = new ConnectToSever(Helper.GetServer(m_context));
            string resultOfCarDetail; CarDetails res;
            try
            {
                resultOfCarDetail = api.ComfirmLogginOn(m_driver);
            }
            catch
            {
                Android.Util.Log.Info("EXC_conflogon1", "confirm logging on failed");
                throw;
            }
            try
            {
                res = Newtonsoft.Json.JsonConvert.DeserializeObject<CarDetails>(resultOfCarDetail);
            }
            catch (Exception ex)
            {
                Android.Util.Log.Info("EXC_conflogon2", "deserialize confirm logging on failed\n" + ex.Message);
                throw;
            }

            if (res.carExists != true)
            {
                MyMessageBox.SetAlertBox("Opps!!!!!!!!", "This Car Number Was Wrong!!!!", "OK", m_context);
            }
            else
            {
                string carType = res.carType;
                string seatNum = res.numOfSeats.ToString();
               // MainActivity act = new MainActivity( result.driverId );
                var mact = new Intent(m_context,typeof(MainActivity) );
                mact.PutExtra( "driverID",  result.driverId.ToString() );
                MyMessageBox.SetAlertBox("Comfirm!", "Your car is a: " + carType + " with " + seatNum + " seats??", "Yes", "No", mact,m_context);


            }
        }

        /*private void ChangeDriverStatues()
        {

        }*/

        protected override void OnPreExecute()
        {
            base.OnPreExecute();
            processDialog = ProgressDialog.Show( m_context, "Driver Loging On...", "Please Wait...", true, true);
        }

        protected override Object DoInBackground(params Object[] @params)
        {
            var api = new ConnectToSever(Helper.GetServer(m_context));

            string res = string.Empty;
            try
            {
                res = api.DriverLogingOn(m_driver);
            }
            catch
            {
                _resterror = true;
                Android.Util.Log.Info("EXC_dlogon1", "driver logon failed");
                return -1;
            }
            return res;
        }

        protected override void OnPostExecute(Object result)
        {
            base.OnPostExecute(result);
            //hide and kill the progress dialog
            processDialog.Hide();
            processDialog.Cancel();

            if (_resterror == true)
            {
                Android.Util.Log.Info("EXC_dlogon2", "logon connection has failed, noop");
                return;
            }

            DriverLogonResult resDriverDetail;
            try
            {
                resDriverDetail = Newtonsoft.Json.JsonConvert.DeserializeObject<DriverLogonResult>(result.ToString());
            }
            catch (Exception ex)
            {
                Android.Util.Log.Info("EXC_dlogon3", "logon deser has failed, noop\n" + ex.Message);
                return;
            }

            if (resDriverDetail.logonSuccess)
            {
                this.SetComfirmAlertBox( m_driver.carNum, resDriverDetail );  
            }
            else
            {
                MyMessageBox.SetAlertBox("Wrong!", "Wrong username or password!!!", "OK!",m_context);
            }
        }
    }

    [Activity(Label = "MyDriver-Driver", MainLauncher = true, Icon = "@drawable/icon")]
    public class Activity1 : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            Android.Util.Log.Info("EXC_logstart", "**************** starting driver module ****************");

            // Get our button from the layout resource,
            // and attach an event to it
            EditText eTextUsername = FindViewById<EditText>(Resource.Id.UserNameBox);
            EditText eTextPassword = FindViewById<EditText>(Resource.Id.PasswordBox);
            EditText eTextCarNum = FindViewById<EditText>(Resource.Id.CarNumBox);
            Button viewPrefsBtn = FindViewById<Button>(Resource.Id.BtnViewPrefs);
            Button button = FindViewById<Button>(Resource.Id.MyButton);

            button.Click += delegate 
            {
                if (eTextCarNum.Text != "" && eTextPassword.Text != "" && eTextUsername.Text != "")
                {
                    DriverLogon driver = new DriverLogon();
                    driver.userName = eTextUsername.Text;
                    driver.password = eTextPassword.Text;
                    driver.carNum = eTextCarNum.Text;
                    DriverLogonAsync asyDriver = new DriverLogonAsync(this, driver);
                    asyDriver.Execute();
                }
            };

            viewPrefsBtn.Click += (sender, e) =>
            {
                StartActivity(typeof(PreferencesActivity));
            };
        }
    }
}
4

3 回答 3

1

您的登录活动可能是您的第一个活动。不要那样做,让你的主要活动检查用户是否已经输入了登录凭据,如果没有 - 从中​​触发登录活动。

这样,当您完成登录并返回主要活动时,返回将带您回到主屏幕。当您运行应用程序时,您将获得主要活动。

于 2012-10-06T22:20:30.300 回答
0

登录屏幕:如果用户已登录(例如:您可以使用 SharedPreferences 来保存状态)然后移动到主要活动,然后在登录屏幕上调用 finish() 以完成该活动。它应该工作。

正如你所说,它在完成()时崩溃。您应该查看错误日志为什么它在调用完成时崩溃并修复它。

于 2012-10-06T22:34:00.080 回答
0

您是否考虑过简单地通过 保存登录信息SharedPreferences,并且登录信息的检查已经存在于登录活动的共享首选项中?如果数据已经存在,您可以简单地将用户推送到主页。

于 2012-10-06T22:21:04.073 回答