简体   繁体   English

onPause() onStop() onResume() 之间的混淆

[英]Confusion between onPause() onStop() onResume()

I am developing an Android app in which I want to check if the user has minimized the application or just come from another activity.我正在开发一个 Android 应用程序,我想在其中检查用户是否已最小化应用程序或只是来自其他活动。

In detail, if the user have started another app, went to the home screen or locked the screen, I want to show the activity where the user will enter the password to access the app.详细地说,如果用户启动了另一个应用程序,进入了主屏幕或锁定了屏幕,我想显示用户将输入密码访问应用程序的活动。 But where or how to check this exactly?但是在哪里或如何确切地检查这一点?

https://developer.android.com/guide/components/activities/activity-lifecycle.html https://developer.android.com/guide/components/activities/activity-lifecycle.html

I was trying onResume() but according to documentation onResume() can be fired if the user's navigating to another activity and coming back.我正在尝试onResume()但根据文档onResume()如果用户导航到另一个活动并回来,则可以触发。

I'm not very clear on what you are trying to achieve.我不是很清楚你想要达到的目标。 The life cycle diagram is quite clear if you are wondering which lifecycle method it would hit when something happens.如果您想知道发生某些事情时它会使用哪种生命周期方法,那么生命周期图就非常清楚了。 Basically, it's the same to minimise the app and go to another activity.基本上,最小化应用程序并转到另一个活动是相同的。 But if you are referring to coming from another activity in your own app, you can distinguish your own activity by adding extra information to the intent you use.但是,如果您指的是来自您自己应用程序中的另一个活动,您可以通过向您使用的意图添加额外信息来区分您自己的活动。

Basically, it's like this:基本上,它是这样的:

Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
intent.putExtra(key,value);
startActivity(intent);

And in your SecondActivity, you can always retrieve that data like this:在你的 SecondActivity 中,你总是可以像这样检索数据:

Bundle bundle = getIntent().getExtras();
if ( bundle != null && bundle.containsKey(key) ) {
    value = bundle.getInt(key); // not nessecarily getInt(), you should use according to your value type
    // use the value to tell if it is from your own app
} else {
    // it is not from your own app
}

You can use this mechanism combined with the lifecycle methods.您可以将此机制与生命周期方法结合使用。 For example, if you use the latter code in your onCreate() method, then whenever the Activity is created, if will check who creates it, which sounds like your what you might want.例如,如果您在 onCreate() 方法中使用后面的代码,那么无论何时创建 Activity,if 都会检查谁创建了它,这听起来像是您想要的。

As soon as your activity becomes visible it will call OnStart() and as soon as it is ready for the interaction(such as touch ,click etc event).一旦您的活动变得可见,它就会调用 OnStart() 并且一旦准备好进行交互(例如触摸、点击等事件)。 it calls onResume, at this stage your app is running and it is completely in foreground.它调用 onResume,在这个阶段你的应用程序正在运行并且它完全处于前台。 When your activity start another activity or a dialog box then it calls onPause it means activity is visible but user can not interact with the Activity UI.当您的活动启动另一个活动或对话框时,它会调用 onPause,这意味着活动是可见的,但用户无法与活动 UI 进行交互。 in case we start another Activity which completely hides the previous activity then its onStop method is called如果我们启动另一个完全隐藏前一个活动的活动,则调用其 onStop 方法

onPause : Called when another activity comes into the foreground. onPause :当另一个活动进入前台时调用。

onStop : Called when that other activity is completely visible. onStop :当其他活动完全可见时调用。

onResume : Called when your activity is navigated back to from the onPause state. onResume :当您的活动从onPause状态导航回时调用。

Maybe your app was already in the onStop state, so then it would call onRestart .也许你的应用程序已经处于onStop状态,所以它会调用onRestart

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

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