19

Sto tentando di aprire un frammento quando si preme una notifica nella barra di notifica. La mia struttura app è:Come aprire la pagina di frammento, quando si preme una notifica in Android

  • un'attività di base con un menu cassetto nav
  • qualche frammento che si aprono dal menù

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

Qualcuno può dirmi come collegare con frammento successivo pagina (il codice presente è in classe che estende il frammento)

+0

Broadcast Receiever? http://developer.android.com/reference/android/content/BroadcastReceiver.html – James

+0

Potresti compilarlo, sono nuovo in Android –

risposta

24

Avrai bisogno di avviare la tua attività di base come al solito, ma aggiungere alcuni un'informazione all'intento su quale frammento di menu verrà aperto. Qui puoi vedere come può essere fatto: https://stackoverflow.com/a/8610916/1652236

Questo dipende dalle informazioni extra che si recuperano nel metodo delle attività 'onCreate()' in cui si utilizzerà per avviare/caricare il frammento.

vedi qui per esempio, come il lavoro con i frammenti: http://www.tutorialspoint.com/android/android_fragments.htm http://developer.android.com/guide/components/fragments.html

E 'intento di lanciare questa procedura sarà qualcosa del tipo:

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

e nella vostra attività di base:

@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

Qui in questa app ho bisogno di generare il mio messaggio come notifica e l'ho tenuto pulsante in una delle opzioni di menu sulla barra delle azioni, ho usato solo un'attività e resto tutti sono frammenti –

+0

Come ho capito, è necessario avviare l'attività di base con qualche frammento. E il tipo di frammento deve dipendere dal tipo di notifica. È vero? – Vito

+0

Non tutti i frammenti, solo alcune pagine di frammenti dovrebbero dipendere dal tipo di notifica –

2

è inoltre necessario aggiungere .commit(); e ft1.addToBackStack (null); in modo che non si sovrapponga a quello precedente e se non lo si annota questo ft1.addToBackStack (null); sulla schiena la vostra applicazione uscirà in modo da aggiungere in base alle proprie funzionalità

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) 

Come il vostro intento set Flags: FLAG_ACTIVITY_SINGLE_TOP, "onCreate()" non sarà chiamato quando è stata creata l'attività, si dovrebbe ricevere invece i parametri nel metodo chiamato "onNewIntent()".