简体   繁体   English

带有startActivityForResult的android launchMode singleTask

[英]android launchMode singleTask with startActivityForResult

We have a requirement to force the user to set up a password the first time he/she starts the app. 我们有一项强制要求用户在首次启动应用程序时设置密码。 So we have our main activity with launchMode=singleTask which start a password setting activity using startActivityForResult . 因此,我们的主要活动是launchMode=singleTask ,它使用startActivityForResult启动密码设置活动。

We also want the user to go back to their home if he/she taps back from the password setting page so we put the code to finish the main activity in its onActivityResult if it receives RESULT_CANCELLED . 如果用户从密码设置页面上点击回来,我们还希望用户返回自己的家,因此,如果代码收到RESULT_CANCELLED ,我们会将代码放入其主要活动中的onActivityResult However, if the user taps home and re-enter our app, we want to show the password setting page again. 但是,如果用户点击家并重新进入我们的应用,我们想再次显示密码设置页面。 But in this case, it will be destroyed (because main activity's launchMode is singleTask ) and also returns RESULT_CANCELLED to our main activity causing it to be finished. 但是在这种情况下,它将被销毁(因为主要活动的launchModesingleTask ),并且还向我们的主要活动返回RESULT_CANCELLED使其完成。

So the problem is from main activity, we cannot distinguish between tapping back and tapping home then re-enter the app. 因此,问题出在主要活动上,我们无法区分是点击还是点击,然后重新进入应用。

Is there anyway to fulfill this requirement while still keeping the launchMode as singleTask ? 无论如何要满足此要求,同时仍将launchMode保持为singleTask

onBackPressed() for tapping back onBackPressed()用于回击

Called when the activity has detected the user's press of the back key. 当活动检测到用户按下返回键时调用。 The default implementation simply finishes the current activity, but you can override this to do whatever you want. 默认实现只是完成当前活动,但是您可以覆盖此活动以执行所需的任何操作。

http://developer.android.com/reference/android/app/Activity.html#onBackPressed%28%29 http://developer.android.com/reference/android/app/Activity.html#onBackPressed%28%29

&& onPause() for home screen && onPause()用于主屏幕

(though it can be due to other things also like your next activity or switching to another application) http://developer.android.com/reference/android/app/Activity.html#onPause%28%29 (尽管可能是由于其他原因(例如您的下一个活动或切换到另一个应用程序)引起的) http://developer.android.com/reference/android/app/Activity.html#onPause%28%29

Update : 更新:

Why Don't you use Sharedpreferences and check if the app is run for the first time and password/rest fields are set rather than using the approach you are using right now . 为什么不使用Sharedpreferences并检查应用程序是否是首次运行且是否设置了密码/剩余字段,而不是使用当前使用的方法。

I had a very similar issue recently. 我最近有一个非常相似的问题。

What I would suggest is that you don't actually finish the app when you get the RESULT_CANCELLED . 我的建议是,当您获得RESULT_CANCELLED时,您实际上并没有完成应用程序。

Instead do something like: 而是执行以下操作:

protected void onActivityResult(int requestCode, int resultCode, Intent intent) {   

    if (resultCode == Activity.RESULT_CANCELED) {

        if (/*TEST IF A USER IS SIGNED IN*/) {

            //IF SIGNED IN PROCEED TO MAIN ACTIVITY
        }
    // OTHERWISE FORCE THEM TO SIGNIN/REGISTER/WHATEVER AGAIN 
    // WITH ANOTHER CALL TO startActivityForResult
    ...

This is a back of an envelope approach to your problem, but this pattern has worked for me. 这是解决您的问题的解决方法的后盾,但是这种模式对我有用。

For me, my Authentication/Registration is all done in an Abstarct Class that extends Activity . 对我来说,我的身份验证/注册都是在扩展ActivityAbstarct Class中完成的。 Each of my activities then extends this class. 然后,我的每项活动都会扩展该课程。 I Place the Auth/Reg method calls in the onResume() of this Abstarct Class . 我将Auth / Reg方法调用放在此Abstarct ClassonResume()中。

When a startActivityForResult is launched in the onResume() method and fails I don't finish() the activity but just let the app proceed and let the class handle which Reg/Auth form to present. 当在onResume()方法中启动startActivityForResult并失败时,我没有finish()活动,只是让应用继续进行,让类处理要呈现的Reg / Auth表单。 Which it will do by checking various flags to determine what action it is meant to be performing. 它会通过检查各种标志来确定它打算执行的操作,从而执行该操作。

The added benefit of this approach is that each and every Activity that might extend this abstract class gets the added benefit of a security check each time it is used. 这种方法的附加好处是,可能扩展此抽象类的每个Activity每次使用时都会获得安全检查的附加好处。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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