[英]How to open a section in a tabbed activity using an intent from another activity?
我有一個選項卡式活動,其側邊欄有4個部分。
當我從另一個活動到該活動執行Intent時,我希望它在第四部分而不是第一部分(默認)中處於打開狀態。
如何更改為在第四部分打開?
嘗試這個:
當您從另一個活動發送意圖時,請在該意圖中放入一個布爾捆綁,當您的活動收到意圖時,請檢查捆綁是否為真,如果是,則打開您的第四部分。
您的發送活動
Bundle bundle=new Bundle();
bundle.putBoolean("fourthSection",true);
startActivity(new Intent(currentActivity.this,otherActivity.class).putExtras(bundle));
您的收貨活動
Bundle bundle=getIntent.getExtras();
if(bundle.getBoolean("fourthSection")){
//go to your fourth section
}
這是您的操作方式:
public class OtherActivityThatHasYourTabs ... {
public static final String TAB_INDEX_KEY = "tab_index";
...
}
有一個名為TAB_INDEX_KEY
的密鑰。 這就是獲取和設置在這兩個活動之間傳遞的選項卡索引的方式。
在您的“當前”活動中,執行以下操作:
Intent otherActivity = new Intent(CurrentActivity.this,OtherActivityThatHasYourTabs.class);
otherActivity.putExtra(OtherActivityThatHasYourTabs.TAB_INDEX_KEY,4);
startActivity(otherActivity);
在您的OtherActivityThatHasYourTabs
,您可以通過getIntent()
檢索用於啟動它的Intent
。 因此,您可以這樣做:
Intent intent = getIntent();
Bundle extras = intent.getExtras();
int tabIndex = extras.getInt(TAB_INDEX_KEY);
if( tabIndex != 0 ){
yourMethodToDisplayTabAtIndex( tabIndex );
}
從1
開始索引,因為如果找不到傳遞給getInt()
的鍵,則getInt()
返回0
。
使用int
可以提高您的整潔度和靈活性,因為您只管理一個鍵,並且如果添加更多標簽,則可以增加索引。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.