Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 (i.e. it is the Main Activity that is started when I started the application).

  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. What does getIntent() return if I call it on the very first activity?? 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.
like image 952
stedy Avatar asked Dec 13 '25 13:12

stedy


1 Answers

Yes, getParentActivityIntent()

Obtain an Intent that will launch an explicit target activity specified by this activity's logical parent. The logical parent is named in the application's manifest by the parentActivityName attribute. 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.

Returns a new Intent targeting the defined parent of this activity or null if there is no valid parent.

like image 191
Tomer Mor Avatar answered Dec 16 '25 02:12

Tomer Mor