Android opens a special tab fragment when a notification is pressed

I have an Android app that uses Action Bar tabs. There is also a notification system.

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

Below is the main activity code for the 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 a service code that displays a 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); } 
+4
source share
2 answers

Click the tab indicating the action to open, as well as the intention while waiting for the intention. And in your operation of extracting actions (see GetIntent ()), and based on this, 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 } 
+4
source

Find my answer here: Run the snippet in the Android app from the notification bar . I do not think this is very different from adding extra to your Intent , but it is useful to use Android action -idea.

Submit your notification function action:

 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 run the corresponding snippet.

0
source

Source: https://habr.com/ru/post/1479091/


All Articles