0

私のアプリケーションでは、ユーザーはログインしてアプリケーションにアクセスする必要があります。私の問題は、ユーザーがメインアクティビティから戻るキーを押すと、ユーザーがログイン画面に送り返されることです。

ht OnBackPressed() をオーバーライドできることはわかっていますが、これで問題は解決しません。OnBackPressed() をオーバーライドしてユーザーをホーム画面に送ると、ユーザーがアプリを再度開いたときにログイン画面が表示されます。ユーザーがログイン画面で表示されていても、ユーザーはバックグラウンドでログインしています。そのため、メイン アクティビティに移動するには、ユーザーは再度ログインする必要があります。

Finish() を試しましたが、mono droid でエラーが表示されます。

ログイン アクティビティ コードを添付しました。誰かが私をこの問題から解放してくれるかもしれません。

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で、ログイン アクティビティの共有設定にログイン情報の確認が既に存在することを検討しましたか? データが既に存在する場合は、ユーザーをメイン ページにプッシュするだけです。

于 2012-10-06T22:21:04.073 に答える
0

ログイン画面: ユーザーがログインしている場合 (例: 状態を保存するために SharedPreferences を使用できます)、メイン アクティビティに移動し、ログイン画面で finish() を呼び出してそのアクティビティを終了します。それはうまくいくはずです。

あなたが言ったように、finish()でクラッシュします。エラー ログを調べて、finish の呼び出し時にクラッシュする理由を確認し、それを修正する必要があります。

于 2012-10-06T22:34:00.080 に答える