19

我试图在通知栏中按通知时打开片段。我的应用程序的结构是:如何在android中按下通知时打开片段页面

  • 与导航抽屉菜单
  • 一些片段被从菜单中打开一个基地活动

    b.setOnClickListener(new OnClickListener() { 
    
         @SuppressWarnings({ "deprecation", "static-access" }) 
         public void onClick(View v) { 
    
         w_nm=(NotificationManager) getActivity().getSystemService(getActivity().NOTIFICATION_SERVICE); 
    
         Notification notify=new Notification(R.drawable.notnificationlogo,waternoti,System.currentTimeMillis()); 
    
         Intent notificationIntent = new Intent(getActivity(), Abc.class); 
    
    
    
         PendingIntent pending=PendingIntent.getActivity(getActivity(), 0,notificationIntent, 0); 
    
    
         notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP 
           | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
    
         notify.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL; 
    
          notify.setLatestEventInfo(getActivity(),waternoti,waternoti1, pending); 
    
         w_nm.notify(0, notify); 
    

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

+0

Broadcast Receiever? http://developer.android.com/reference/android/content/BroadcastReceiver.html – James 2014-10-28 12:43:29

+0

你能否详细说明一下,我是新手机android – 2014-10-28 12:47:00

回答

24

您将需要像往常一样开始基本活动,但添加一些extr关于将打开什么菜单片段的意图的信息。 在这里你可以看到它是如何完成的: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); 
    } 
} 
+0

在这个应用程序中,我需要生成我自己的消息作为通知,我保留按钮在操作栏上的其中一个菜单选项中,我只使用了一个活动,其余都是碎片 – 2014-10-28 13:00:34

+0

正如我所理解的,您需要用一些片段开始您的基本活动。片段类型必须依赖于通知类型。这是真的吗? – Vito 2014-10-28 13:02:45

+0

不是所有的片段,只有几个片段页面应该取决于通知类型 – 2014-10-28 13:09:16

2

您还应该添加.commit();ft1.addToBackStack(null);,以便它不会重叠在prevoius上,并且如果您不会添加这个ft1.addToBackStack(null);上回您的应用程序将退出,以便根据自己的功能添加此

String menuFragment = getIntent().getStringExtra("menuFragment"); 

ft1 = getSupportFragmentManager().beginTransaction(); 

ft1.addToBackStack(null); 

ft1.replace(R.id.frame_container, favoritesFragment).commit(); 
4
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP 
      | Intent.FLAG_ACTIVITY_SINGLE_TOP) 

当你的意图设置的标志:FLAG_ACTIVITY_SINGLE_TOP“的onCreate()”将不会被调用的活动已创建的时候,你应该而是使用名为“onNewIntent()”的方法接收参数。

相关问题