我正在尝试根据以下内容实施导航:http://developer.android.com/training/implementing-navigation/ancestral.html
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case 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);
}
return true;
}
return super.onOptionsItemSelected(item);
}
但if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
总是返回FALSE。
这段代码有用吗?它在哪些情况下会返回true?
答案 0 :(得分:4)
在pre-Jelly Bean设备上,source code of NavUtils,shouldUpRecreateTask
是:
public boolean shouldUpRecreateTask(Activity activity, Intent targetIntent) {
String action = activity.getIntent().getAction();
return action != null && !action.equals(Intent.ACTION_MAIN);
}
使用Activity
的操作(即ACTION_VIEW
等)确定此Activity
是否是从外部源启动的。在Jelly Bean +设备上,它使用Activity.shouldUpRecreateTask
(source code):
public boolean shouldUpRecreateTask(Intent targetIntent) {
try {
PackageManager pm = getPackageManager();
ComponentName cn = targetIntent.getComponent();
if (cn == null) {
cn = targetIntent.resolveActivity(pm);
}
ActivityInfo info = pm.getActivityInfo(cn, 0);
if (info.taskAffinity == null) {
return false;
}
return !ActivityManagerNative.getDefault()
.targetTaskAffinityMatchesActivity(mToken, info.taskAffinity);
} catch (RemoteException e) {
return false;
} catch (NameNotFoundException e) {
return false;
}
}
使用Task Affinity来确定给定Activity
的亲和力是否与启动时的亲和力相同(即,如果Gmail启动您的Activity
,则会给出Gmail的亲和力,而不是自己的亲和力。
NavUtils.navigateUpTo
,在所有情况下,都会启动一项活动。如果它没有启动相应的Activity
,那么您可能需要查看您正在使用的启动模式,并提供有关它无法运行的平台版本的详细信息。
答案 1 :(得分:0)
我通过使用ActivityManager并检查backStack条目来解决它。
我希望它对你们所有人都有用。
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
ActivityManager am = (ActivityManager) getApplicationContext().getSystemService(getApplicationContext().ACTIVITY_SERVICE);
List<RunningTaskInfo> runningTaskInfoList = am.getRunningTasks(10);
List<String> backStack = new ArrayList<String>();
Iterator<RunningTaskInfo> itr = runningTaskInfoList.iterator();
while(itr.hasNext()){
RunningTaskInfo runningTaskInfo = (RunningTaskInfo)itr.next();
String topActivity = runningTaskInfo.topActivity.getShortClassName();
backStack.add(topActivity.trim());
}
if(backStack!=null){
if(backStack.get(1).equals(".MainActivity")){
moveTaskToBack(true); // or finish() if you want to finish it. I don't.
} else {
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
}
}
break;
}
}
我需要使用:
<uses-permission android:name="android.permission.GET_TASKS" />
在我的清单中。
虽然不是我想要的解决方案,但效果很好。 不幸的是,android仍然缺乏先进的内置方法来实现简单的事情,我们都必须找到所有方法。