简体   繁体   English

如何知道为什么调用onStart()

[英]How to know why onStart() is called

My question is simple, how can i know if the onStart() method got called because my activity got created or because it got started by another activity through an intent? 我的问题很简单,我怎么知道onStart()方法是由于创建我的活动而被调用,还是因为它是由另一个活动通过意图启动的?

I guess its not specifically the onStart() method but any of the activity lifecycle methods that gets called when creating an activity. 我猜不是它的onStart()方法,而是创建活动时调用的任何活动生命周期方法。 Thanks. 谢谢。

use an extra intent for checking whenever your activity got created or got started by another activity by intent : 使用额外的intent来检查您的活动是由intent创建还是由另一个活动开始的:

public static final EX_INFO = "from_another";
private boolean isFromAnotherActivity = false;

and when you start from another activity by intent, just pass the intent with: 当您通过意图从另一个活动开始时,只需将意图传递给:

intent.putExtra(EX_INFO, true)

read the info from your onStart method 从您的onStart方法中读取信息

Intent intent = getExtraIntent();
isFromAnotherActivity = intent.getBooleanExtra(EX_INFO, false);

onCreate-> onStart-> onResume. onCreate-> onStart-> onResume。 Genrally this process is followed when an Activity is Created. 通常,创建活动时将遵循此过程。 If you want to get notified that your Activity is called by an Intent, pass any variable using putExtra from calling activity and then check whether it has some value in getExtra in Called Activity. 如果您想收到有关Intent调用了Activity的通知,请使用来自调用Activity的putExtra传递任何变量,然后检查Called Activity中的getExtra是否具有某些值。

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

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