我有一個使用操作欄選項卡的Android應用程序。還有一個通知系統。通知點擊Android開放特定選項卡片段
我想在點擊通知時直接打開特定的選項卡。如何做到這一點(因爲通知掛起的意圖只能打開活動,而我的主要活動包含3個片段的3個片段)?
以下是標籤主要活動的代碼。
public class MaintabActivity extends Activity {
public static Context appContext;
public static MapView mMapView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainsc);
appContext = getApplicationContext();
startService(new Intent(this, MyService.class));
//ActionBar
ActionBar actionbar = getActionBar();
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab ChatTab = actionbar.newTab().setText("Chat");
ActionBar.Tab MapsTab = actionbar.newTab().setText("Maps");
ActionBar.Tab SplashTab=actionbar.newTab().setText("Splash");
Fragment ChatFrag = new ChatActivity();
MapActivity mMapFragment = MapActivity.newInstance();
Fragment SplashFrag = new SplashActivity();
ChatTab.setTabListener(new MyTabsListener(ChatFrag));
MapsTab.setTabListener(new MyTabsListener(mMapFragment));
SplashTab.setTabListener(new MyTabsListener(SplashFrag));
actionbar.addTab(ChatTab);
actionbar.addTab(MapsTab);
actionbar.addTab(SplashTab);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("tab", getActionBar().getSelectedNavigationIndex());
}
}
class MyTabsListener implements ActionBar.TabListener {
public Fragment fragment;
public MyTabsListener(Fragment fragment) {
this.fragment = fragment;
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
Toast.makeText(MaintabActivity.appContext, "Reselected!", Toast.LENGTH_LONG).show();
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
ft.replace(R.id.fragment_container, fragment);
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
ft.remove(fragment);
}
}
這是顯示通知的服務中的代碼。
private void showNotification() {
CharSequence text = getText(R.string.local_service_started);
// Set the icon, scrolling text and timestamp
Notification notification = new Notification(R.drawable.ic_launcher, text,
System.currentTimeMillis());
// The PendingIntent to launch our activity if the user selects this notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MaintabActivity.class), 0);
// Set the info for the views that show in the notification panel.
notification.setLatestEventInfo(this, getText(R.string.local_service_label),
text, contentIntent);
// Send the notification.
mNM.notify(NOTIFICATION, notification);
}
@ user2319636-post some code。 – TheFlash 2013-05-04 04:43:23
代碼是什麼?它的常規操作欄選項卡,我想打開假設標籤乙點擊通知。 – user2319636 2013-05-04 05:16:21
user2319636 - 當你點擊通知時,它會調用你的包含Tabs的activity,你可以通過viewPager調用你的特定片段 - viewPager.setCurrentItem(position); – TheFlash 2013-05-04 05:19:25