问题
I have an activity that starts another activity.
Is it mandatory that I specify the parent activity in the Android Manifest?
Im asking this because there might be other activities that will start this one as well, so should I specify all of them?
android:parentActivityName="com.example.myfirstapp.MainActivity"
回答1:
As per docs -> section android:parentActivityName
:
The system reads this attribute to determine which activity should be started when the user presses the Up button in the action bar. The system can also use this information to synthesize a back stack of activities with TaskStackBuilder.
So you only need to specify that if you're going to use up-navigation (as opposed to navigation by back button) or TaskStackBuilder. In other cases, you don't need it.
Check in here about up-navigation: http://developer.android.com/design/patterns/navigation.html
回答2:
While it should be defined if upward navigation or backstack synthesis is desired, note that the attribute android:parentActivityName
was introduced in API Level 16.
For prior releases, parent activity information is accessed from attributes defined inside of a <meta-data>
tag that is declared inside of the child <activity>
tag.
Example:
<activity
android:name=".DetailActivity"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity"/>
</activity>
Inside of the <meta-data>
tag, set the android:name
attribute to android.support.PARENT_ACTIVITY
, and the android:value
attribute to the parent activity class name (i.e. the same class name as that assigned to android:parentActivityName
).
Unless API level is known, both the <meta-data>
and inline specifications are recommended.
For more on specifying the parent activity, see: https://developer.android.com/training/implementing-navigation/ancestral.html#SpecifyParent
In addition, consider defining the android:launchMode
attribute inside of your main <activity>
tag to set the desired behavior of upward navigation: https://developer.android.com/guide/topics/manifest/activity-element.html#lmode
回答3:
You don't necessarily need to define the parentActivity
in the AndroidManifest.xml
. You can use the below code for the back navigation enabled:
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
And implement this:
public boolean onOptionsItemSelected(MenuItem item) {
int itemId = item.getItemId();
if (itemId == android.R.id.home) {
onBackPressed();
}
return super.onOptionsItemSelected(item);
}
But if you define the parentActivity
in the manifest, then the system reads this attribute to determine which activity should be started when the user presses the Up button in the action bar. i.e, it will create a new instance of the parentAcivity
, means it will call the onCreate()
of the parent activity.
回答4:
You have to specify every Activity in the manifest which you call via Intent or Launchers, that the system can find it. So, mark one Activity as the Launcher that your App can get started and register every other Activity, which you call in your App.
If you have a BaseActivity like this:
public class BaseActivity extends Activity{}
public class MyActivity extends BaseActivity{}
than you only have to register MyActivity, because BaseActivity is not called by the system but you.
回答5:
No its not necessary to specify parent activity in manifest
like this
android:parentActivityName="com.example.myfirstapp.MainActivity"
for navigationUp
you can also use setDisplayHomeAsUpEnabled(true);
and onSupportNavigateUp()
method Take a Look at this
来源:https://stackoverflow.com/questions/19207762/must-i-specify-the-parent-activity-name-in-the-android-manifest