简体   繁体   English

销毁活动如何工作?

[英]How destroy activity works?

I'm new in android development, and there is something about the life cycle activity that I don't understand, especially with the following example of application that i'm working on. 我是android开发的新手,我不了解生命周期活动,尤其是下面我正在处理的应用程序示例。

In my app, I have a Login activity and a Main activity. 在我的应用程序中,我有一个Login活动和一个Main活动。

  1. In my Login activity, with successful attempt, there is a Intent that start the main activity, and finish() the login activity. 在我的登录活动中,尝试成功,有一个Intent启动主活动,并完成()登录活动。
    ==> There, my login activity is destroyed, so this should not show up again. ==>在那里,我的登录活动已被破坏,因此不应再次显示。

     Intent intent = new Intent(this, MainActivity.class); intent.putExtra("authentResult", tokenDto); startActivity(intent); finish(); //destroy activity to not open it with back button` 
  2. In my Main activity, I have a disconnect button that create an Intent that start a (new ?) login activity. 在我的主要活动中,我有一个断开连接按钮,用于创建启动(新?)登录活动的Intent。
    ==> Until there, everything's normal, and the login activity is displyed. ==>到此为止,一切正常,并且将显示登录活动。

     Intent loginActivity = new Intent(this, LoginActivity.class); 

    startActivity(loginActivity); startActivity(loginActivity);

  3. In the login activity, using the Back button should close the app. 在登录活动中,使用“后退”按钮应关闭该应用程序。
    To do that, I send an intent with special flag to the main activity to finish it (So the back button will not wake up the main activity), and then I finish the login activity. 为此,我向主要活动发送带有特殊标志的意图以完成它(因此后退按钮不会唤醒主要活动),然后完成登录活动。 The onDestroy method is called and I see the login window close itself. 调用了onDestroy方法,我看到登录窗口关闭了。
    ==> From here I expect the app to be closed. ==>我希望从这里关闭该应用程序。 But a "new" login activity shows up, and i suspect that it would be the activity of the first point , so I'm a little lost there... 但是出现了“新”登录活动,我怀疑这是第一点的活动 ,所以我在这里有点迷路了...

     public void onBackPressed() { Log.d(TAG, "BACK PRESSED - loginActivity"); //Finish MainActivity Intent intent = new Intent(getApplicationContext(), MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.putExtra("EXIT", true); startActivity(intent); finish(); // finish login activity } 

In the onCreate in the mainActivity, I begin with this : 在mainActivity的onCreate中,我从以下内容开始:

       if (getIntent().getBooleanExtra("EXIT", false)) {
             finish();
       }

Do anyone could explain to me what I'm missing, or show me a better way to close the app directly ? 有人可以向我解释我所缺少的内容,还是向我展示直接关闭该应用程序的更好方法?

Don't hesitate to telle me if something's not clear. 如果有不清楚的地方,不要犹豫告诉我。

If you declare the Login activity as main activity in the Manifest, if you don't destroy it when you launch the second activity then i think the back button will do all you expect without any additional code, and if you press back key on the login activity it will go to phone home screen 如果您在清单中将Login活动声明为主要活动,如果在启动第二个活动时没有销毁它,那么我认为后退按钮将完成您期望的所有操作,而无需任何其他代码,并且如果按登录活动将转到手机主屏幕

On Android applications is the system that decides when to close/kill application. 在Android应用程序上,是决定何时关闭/杀死应用程序的系统。

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

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