简体   繁体   English

有没有办法知道一项活动是否有父活动?

[英]Is there any way to know if an activity has a parent activity or not?

Upon clicking a button in an activity, I want to display Page1 if this activity has a parent activity and display Page2 if it does not (ie it is the Main Activity that is started when I started the application). 单击活动中的按钮后,如果此活动具有父活动,则显示Page1,如果不活动,则显示Page2(即,当我启动应用程序时,它是启动的主活动)。

  1. Is there any way to know if an activity has a parent activity or not? 有没有办法知道一项活动是否有父活动?
  2. I tried getIntent() from the main but it doesn't crash. 我从main尝试了getIntent()但它并没有崩溃。 What does getIntent() return if I call it on the very first activity?? 如果我在第一个活动上调用它,getIntent()会返回什么? Edit: it crashes when I call getIntent() in OnCreate, but doesn't crash if I call it in a method that is called when I press a button. 编辑:当我在OnCreate中调用getIntent()时崩溃,但如果我在按下按钮时调用的方法中调用它,则不会崩溃。

Yes, getParentActivityIntent() 是的, getParentActivityIntent()

Obtain an Intent that will launch an explicit target activity specified by this activity's logical parent. 获取将启动此活动的逻辑父级指定的显式目标活动的Intent。 The logical parent is named in the application's manifest by the parentActivityName attribute. 逻辑父级由parentActivityName属性在应用程序的清单中命名。 Activity subclasses may override this method to modify the Intent returned by super.getParentActivityIntent() or to implement a different mechanism for retrieving the parent intent entirely. Activity子类可以重写此方法以修改super.getParentActivityIntent()返回的Intent,或者实现完全检索父intent的不同机制。

Returns a new Intent targeting the defined parent of this activity or null if there is no valid parent. 返回以此活动的已定义父项为目标的新Intent,如果没有有效父项,则返回null。

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

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