本篇文章主要介绍了"Android最佳实践之实现高效的应用导航(三) - 提供Up导航",主要涉及到方面的内容,对于Android开发感兴趣的同学可以参考一下:
参考地址:http://developer.android.com/training/implementing-navigation/ancestral.htm...
你可以使用NavUtils的静态方法navigateUpFromSameTask()来实现。这个方法调用后,会启动(或Resume)一个父Activity,如果父Activity在同一个Back任务栈里的话,它将会回到前台。父Activity回到前台是否会调用onNewIntent()取决于:
- 如果父Activity的启动模式是,或者在Intent中设置了FLAG_ACTIVITY_CLEAR_TOP的Flag,那么在父Activity回到前台时会调用onNewIntent()
- 如果父Activity的启动模式是,而且没有设置FLAG_ACTIVITY_CLEAR_TOP的Flag,那么Up导航后会生成一个新的父Activity的实例。
@OverridepublicbooleanonOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home buttoncase android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
returntrue;
}
returnsuper.onOptionsItemSelected(item);
}
navigateUpFromSameTask()这个方法只适用你的App是当前任务栈的所有者(即当前TaskStack从你的App启动)。如果不是这种情况,而且你的App在别的App创建的Task中启动了,则Up导航需要为你的App创建一个新的属于你App的Back任务栈。
导航到一个新的Back栈
如果你的Activity在intent filters 中声明可以被其他的App调用,那么你需要实现onOptionsItemSelected()方法,这样的话,当用户在其他App的任务栈里点击了Up,则你的App会创建一个新的Back栈,然后Resume。
你可以使用NavUtils的shouldUpRecreateTask()方法判断当前Activity是否存在与另一个App的任务栈里。如果返回true,则使用TaskStackBuilder创建一个新的Task,否则可以使用上面描述的navigateUpFromSameTask()方法直接返回。
@OverridepublicbooleanonOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home buttoncase android.R.id.home:
Intent upIntent = NavUtils.getParentActivityIntent(this);
if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
// This activity is NOT part of this app's task, so create a new task// when navigating up, with a synthesized back stack.
TaskStackBuilder.create(this)
// Add all of this activity's parents to the back stack
.addNextIntentWithParentStack(upIntent)
// Navigate up to the closest parent
.startActivities();
} else {
// This activity is part of this app's task, so simply// navigate up to the logical parent activity.
NavUtils.navigateUpTo(this, upIntent);
}
returntrue;
}
returnsuper.onOptionsItemSelected(item);
}
为了让addNextIntentWithParentStack()方法有效,需要在manifest中为Activity声明父Activity,使用android:parentActivityName (或)。
').addClass('pre-numbering').hide();
$(this).addClass('has-numbering').parent().append($numbering);
for (i = 1; i <= lines; i++) {
$numbering.append($('
').text(i));
};
$numbering.fadeIn(1700);
});
});
以上就介绍了Android最佳实践之实现高效的应用导航(三) - 提供Up导航,包括了方面的内容,希望对Android开发有兴趣的朋友有所帮助。
本文网址链接:http://www.codes51.com/article/detail_282294_2.html