一、問題的由來
我們的首頁底部導航是這樣的
有個需求就是,我們在首頁界面中點啊點啊,點到一個商品詳情頁,加入購物車之後,有個按鈕,可以直接跳回首頁,并轉到購物車選項。可能你會說直接跳回去,然後設定為購物車那一頁不就行了麼,然而并非如此。
二、singleTask介紹
隻有一個執行個體。在同一個應用程式中啟動他的時候,若Activity不存在,則會在目前task建立一個新的執行個體,若存在,則會把task中在其之上的其它Activity destory掉并調用它的onNewIntent方法。其他複雜的介紹就不多說了,可參考http://blog.csdn.net/shinay/article/details/7898492
三、 開始實作
1.設定為singleTask啟動模式
<activity
android:name=".MainTabActivity"
android:launchMode="singleTask"
android:screenOrientation="portrait" />
2.商品詳情中調回去,并且加上标志資訊
Intent intent=new Intent(this,MainTabActivity.class);
intent.putExtra("from", "goodDetail");
startActivity(intent);
3.首頁中重寫onNewIntent,根據标志資訊,轉到指定位置
@Override
protected void onNewIntent(Intent intent) {
if(intent.getStringExtra("from").equals("goodDetail")){
ToShopCart();
}
super.onNewIntent(intent);
}