5

I have an android application which uses Action Bar Tabs. There is also a notification system.

I want to open a specific tab directly, on clicking the notification. How to do this(because notification pending intents can only open activities, and my main activity contains 3 fragments for 3 tabs) ?

Following is the code for the mainactivity for tabs.

    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);
}

    }

This is the code from the service which shows notification.

    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);
    }
Ravi
  • 960
  • 1
  • 18
  • 41
user2319636
  • 61
  • 1
  • 1
  • 4
  • @user2319636-post some code. – TheFlash May 04 '13 at 04:43
  • 1
    Code for what ? Its regular action bar tabs, and I want to open suppose tab B on clicking notification. – user2319636 May 04 '13 at 05:16
  • user2319636-when you click on notification it will call your activity containing Tabs and you can call your specific fragment in viewPager by this--viewPager.setCurrentItem(position); – TheFlash May 04 '13 at 05:19
  • What to do if there are 2 different types of notifications, and I want to open different tabs ? How will the activity know which notification was clicked ? – user2319636 May 04 '13 at 05:33
  • @user2319636-For that you can pass the Tabs position with notification...so that when activity being called it will display according to the position of fragment in view Pager..post some code so that i can help you. – TheFlash May 04 '13 at 05:38
  • @user2319636-Where is your notification code? – TheFlash May 04 '13 at 05:50
  • I just added it. Sorry ! – user2319636 May 04 '13 at 05:52

2 Answers2

5

Pass action specifying tab to open, along with intent in pending intent. And in your activity retrieve action (see getIntent()) passed and based on it, open a specific tab.

Intent intent = new Intent(this, Home.class);
intent.setAction("OPEN_TAB_1");
PendingIntent pending intent = PendingIntent.getService(this, 0, intent, 0);              

// In OnCreate() or depending on implementation  
if(getIntent().getAction().equals("OPEN_TAB_1") {
     // Open Tab
}
Anirudh
  • 389
  • 1
  • 7
0

Find my answer here: Launch a fragment in my Android application from the notification bar . I don't think it's much different than adding extras to your Intent, but it's good to make use of Android's action-idea.

Send the action to your notify function:

public static void notify(Context context, String message, String  action) {

    action = action.toUpperCase();

    //...

    Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());

    if(action != null && launchIntent != null){         
        launchIntent.setAction(action);         
    }

    PendingIntent pendingIntent = PendingIntent.getActivity(context, -1, launchIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    notification.when = System.currentTimeMillis();  
    notification.flags |= Notification.FLAG_AUTO_CANCEL; 

    // Set the notification and register the pending intent to it
    notification.setLatestEventInfo(context, appName, message, pendingIntent);

    // Trigger the notification
    notificationManager.notify(0, notification);
}

And then get the action and launch the appropriate fragment.

Community
  • 1
  • 1
marienke
  • 2,465
  • 4
  • 34
  • 66