如何在android中按下通知时打开片段页面

我在通知栏中按通知时试图打开一个片段。 我的app结构是:

谁能告诉我如何链接下一个片段页面(当前代码在扩展片段的类中)

Solutions Collecting From Web of "如何在android中按下通知时打开片段页面"

您将需要像往常一样启动基本活动,但添加一些额外信息以了解将打开哪个菜单片段的意图。 在这里,您可以看到它是如何完成的: https : //stackoverflow.com/a/8610916/1652236

这取决于您在用于启动/加载片段的活动’onCreate()’方法中检索的额外信息。

请参阅此处,例如如何使用片段: http : //www.tutorialspoint.com/android/android_fragments.htm http://developer.android.com/guide/components/fragments.html

它意图启动此过程将是这样的:

 Intent notificationIntent = new Intent(getActivity(), Abc.class); notificationIntent.putExtra("menuFragment", "favoritesMenuItem"); 

在您的基础活动中:

 @Override protected void onCreate(final Bundle savedInstanceState) { String menuFragment = getIntent().getStringExtra("menuFragment"); FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); // If menuFragment is defined, then this activity was launched with a fragment selection if (menuFragment != null) { // Here we can decide what do to -- perhaps load other parameters from the intent extras such as IDs, etc if (menuFragment.equals("favoritesMenuItem")) { FavoritesFragment favoritesFragment = new FavoritesFragment(); fragmentTransaction.replace(android.R.id.content, favoritesFragment); } } else { // Activity was not launched with a menuFragment selected -- continue as if this activity was opened from a launcher (for example) StandardFragment standardFragment = new StandardFragment(); fragmentTransaction.replace(android.R.id.content, standardFragment); } } 
 notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP ) 

由于你的意图设置Flags:FLAG_ACTIVITY_SINGLE_TOP,在创建活动时不会调用“onCreate()”,你应该在名为“onNewIntent()”的方法中接收params。

你还应该添加.commit();ft1.addToBackStack(null); 这样它就不会在prevoius上重叠,如果你不发布这个ft1.addToBackStack(null); 在你的应用程序将退出,所以根据您的function添加

 String menuFragment = getIntent().getStringExtra("menuFragment"); ft1 = getSupportFragmentManager().beginTransaction(); ft1.addToBackStack(null); ft1.replace(R.id.frame_container, favoritesFragment).commit();