1

Я создал уведомление, и я хочу, чтобы он открыл фрагмент при нажатии. Он работает в целом, но когда у меня есть другой Фрагмент, открытый в моем приложении в то время, он не работает.Уведомление не открывает фрагмент?

Например, у меня есть два фрагмента: NoticiaFrag и EventoFrag. Если я открою приложение и открою NoticiaFrag после нажатия кнопки «домой» устройства, чтобы свести к минимуму приложение и получить уведомление до EventoFrag, при нажатии на уведомление открывается NoticiaFrag, а не EventoFrag.

Для выполнения этой работы мне нужно закрыть приложение с помощью кнопки «Назад». После закрытия приложения он работает нормально. Я думаю, что мне нужно снова открыть приложение, когда я нажимаю «Уведомление», но я не знаю, как это сделать.

Метод, который открывает Фрагменты из Уведомления, находится в CustomDrawerLayout и называется openFrag().

Как я могу решить эту проблему?

Уведомление

public class SendNotification { 

    public SendNotification(Context context, String title, String tickerText, String message, String url) { 
      NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
      PendingIntent contentIntent; 

      Intent intent = new Intent(context, CustomDrawerLayout.class); 

      Bundle bd = new Bundle(); 
      bd.putString("url", url); 
      intent.putExtras(bd); 
      intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 

      contentIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

      NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context) 
        .setSmallIcon(R.drawable.logo) 
        .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)) 
        .setTicker(tickerText) 
        .setContentTitle(title) 
        .setShowWhen(true) 
        .setWhen(System.currentTimeMillis()) 
        .setContentText(message); 

      mBuilder.setContentIntent(contentIntent); 

      Notification notification = mBuilder.build(); 
      notification.flags |= Notification.FLAG_AUTO_CANCEL; 
      notification.vibrate = new long[]{150, 300, 150, 600}; 
      mNotificationManager.notify(AndroidSystemUtil.randInt(), notification);   
     } 
    } 

манифеста

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="br.com.ferpapps.santaluzapp" > 

    <!--Internet --> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 

    <!--GCM Permissions --> 
    <uses-permission android:name="android.permission.WAKE_LOCK" /> 
    <uses-permission android:name="android.permission.GET_ACCOUNTS" /> 
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> 
    <permission  android:name=".permission.C2D_MESSAGE" android:protectionLevel="signature" /> 
    <uses-permission android:name=".permission.C2D_MESSAGE" /> 
    <uses-permission android:name="android.permission.VIBRATE"></uses-permission> 
    <!----> 

    <!-- permissoes extra --> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
    <uses-permission android:name="android.permission.READ_PHONE_STATE" /> 
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> 


    <application 
     android:name=".cv.CustomVolleySingleton" 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" 
     > 

     <!-- GCM --> 
     <receiver 
      android:name=".push.GcmBroadcastReceiver" 
      android:permission="com.google.android.c2dm.permission.SEND" > 
      <intent-filter> 
       <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
       <category android:name="br.com.ferpapps" /> 
      </intent-filter> 
     </receiver> 
     <meta-data android:name="com.google.android.gms.version" 
      android:value="@integer/google_play_services_version" /> 
     <service 
      android:name=".push.GcmIntentService" > 
     </service> 
     <!-- close GCM --> 


     <activity 
      android:name=".act.SplashView" 
      android:label="@string/app_name" 
      android:windowSoftInputMode="adjustPan|adjustResize"> 

      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

     <activity android:name=".menu.CustomDrawerLayout"/> 
     <activity android:name=".act.AreaAlunoMainActivity" /> 

    </application> 
</manifest> 

ActionBarActivity

public class CustomDrawerLayout extends ActionBarActivity implements OnItemClickListener{ 
    private ActionBar ab; 
    private DrawerLayout dl; 
    private ListView lv; 
    private ActionBarDrawerToggle tg; 
    private LinearLayout navdrawer; 

    private List<ItensListView> fragments; 
    private CharSequence tl; //titulo principal 
    private CharSequence tlf; //titulo fragment 

    public static final String APP_NAME = "App"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_custom_drawerlayout); 
     getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.action_bar))); 
     getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
     getSupportActionBar().setCustomView(R.layout.actionbar_custom); 

     init(); 

     openFrag(); 
    } 

    private void openFrag(){ 
     //verifica notificacao e abre o fragment correspondente 
     String url = getIntent().getStringExtra("url") != null ? getIntent().getStringExtra("url") : null; 
     Log.i("URL_NOTIFICACAO->", url != null ? url : ""); 
     if(url != null) { 
      //Open By Notification 
      if (url.equals("Noticias")) { 
       selectedItem(0); 
      } else if (url.equals("Eventos")) { 
       selectedItem(1); 
      } else if (url.equals("Tarefas") || 
        url.equals("Advertencias") || 
        url.equals("Agendas")) { 
       Log.i("Notificacao", url); 
       Log.i("LOGADO NA SESSION", SessionUsuario.isLogged(this) + ""); 

       if(SessionUsuario.isLogged(this)){ 
        Log.i("Notificao Logado", "esta logado na area do aluno"); 
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 
        Fragment frag = AreaAlunoFrag.newInstance(); 
        frag.getArguments().putString("url", url); 
        ft.replace(R.id.fl, frag); 
        ft.addToBackStack(APP_NAME); 
        ft.commit(); 
       } 
      } 
     }else{ 
      Log.i("ENTREI FIRST FRAG->","ENTREEIII"); 
      //if(savedInstanceState == null){ 
       selectedItem(0); 
      //} 
     } 
    } 

    private void init(){ 
     //actionbar 
     onConfigActionBar(); 
     //listview 
     configItensListView(); 
     //drawerlayout 
     dl = (DrawerLayout)findViewById(R.id.dl); 
     navdrawer = (LinearLayout)findViewById(R.id.navdrawer); 
     //listview 
     lv = (ListView)findViewById(R.id.lv);    
     lv.setAdapter(new DrawerLayoutListViewAdapter(this, fragments)); 
     lv.setOnItemClickListener(this); 
     //drawerlayout 
     //dl = (DrawerLayout)findViewById(R.id.dl); 
     //mDrawerRelativeLayout = (RelativeLayout)findViewById(R.id.left_drawer); 
     //actionbardrawertoggle 
     tg = new ActionBarDrawerToggle(this, dl, R.drawable.ic_launcher, R.string.drawer_open){ 
      public void onDrawerClosed(View view) { 
       ab.setTitle(tl); 
       supportInvalidateOptionsMenu(); 
      } 

      public void onDrawerOpened(View view) { 
       ab.setTitle(tlf); 
       supportInvalidateOptionsMenu(); 
      } 
     }; 
     dl.setDrawerListener(tg); 
     tl = tlf = getTitle(); 
    } 

    /** ativa actionbar e botao home na action bar */ 
    private void onConfigActionBar(){ 
     ab = getSupportActionBar(); 
     ab.setDisplayHomeAsUpEnabled(true); 
     ab.setHomeButtonEnabled(true); 
    } 

    @Override 
    public void onConfigurationChanged(Configuration newConfig) { 
     super.onConfigurationChanged(newConfig); 
     tg.onConfigurationChanged(newConfig); 
    } 

    /** necessario */ 
    @Override 
    protected void onPostCreate(Bundle savedInstanceState) { 
     super.onPostCreate(savedInstanceState); 
     tg.syncState(); 
    } 


    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     if (tg.onOptionsItemSelected(item)) { 
       return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

    /** necessario */ 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.custom_drawer_layout, menu); 
     return true; 
    } 

    /** necessario */ 
    @Override 
    public boolean onPrepareOptionsMenu(Menu menu) { 
     boolean status = dl.isDrawerOpen(navdrawer); 
     //menu.findItem(R.id.action_settings).setVisible(!status); 
     return super.onPrepareOptionsMenu(menu); 
    } 

    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position,long id) { 
     Log.i("POSITION->", position + ""); 
     selectedItem(position);  
    } 

    /** seleciona o fragment q sera usado */ 
    private void selectedItem(int position){ 
     FragmentTransaction ft; 
     Fragment frag; 
     switch (position){ 
      case 0: 
       ft = getSupportFragmentManager().beginTransaction(); 
       frag = NoticiaFrag.newInstance(); 
       ft.replace(R.id.fl, frag); 
       ft.addToBackStack(APP_NAME); 
       ft.commit(); 
       break; 
      case 1: 
       ft = getSupportFragmentManager().beginTransaction(); 
       frag = EventoFrag.newInstance(); 
       ft.replace(R.id.fl, frag); 
       ft.addToBackStack(APP_NAME); 
       ft.commit(); 
       break; 
      case 2: 
       ft = getSupportFragmentManager().beginTransaction(); 
       frag = LoginFrag.newInstance(); 
       ft.replace(R.id.fl, frag); 
       ft.addToBackStack(APP_NAME); 
       ft.commit(); 
       break; 
      case 3: 
       ft = getSupportFragmentManager().beginTransaction(); 
       frag = ContatoFrag.newInstance(); 
       ft.replace(R.id.fl, frag); 
       ft.addToBackStack(APP_NAME); 
       ft.commit(); 
       break; 
      case 4: 
       ft = getSupportFragmentManager().beginTransaction(); 
       frag = CompartilhaFrag.newInstance(); 
       ft.replace(R.id.fl, frag); 
       ft.addToBackStack(APP_NAME); 
       ft.commit(); 
       break; 
      case 5: 
       ft = getSupportFragmentManager().beginTransaction(); 
       frag = SobreFrag.newInstance(); 
       ft.replace(R.id.fl, frag); 
       ft.addToBackStack(APP_NAME); 
       ft.commit(); 
       break; 
      default: 
       closeApp(); 
       break; 
     } 
     lv.setItemChecked(position, true); 
     setCustomTitle(fragments.get(position).getTexto()); 
     dl.closeDrawer(navdrawer); 
    } 

    /** define o titulo da actionbar */ 
    private void setCustomTitle(String title){ 
     //SpannableString s = new SpannableString(title); 
     // s.setSpan(new TypefaceSpan(this, BatalhaConfigs.FONT), 0, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
     //ab.setTitle(s); 
     //tl = s; 
    } 

    @Override 
    public void onBackPressed() { 
     //if(getSupportFragmentManager().getBackStackEntryCount() > 0){ 
     // getSupportFragmentManager().popBackStackImmediate(); 
     // }else{ 
      super.onBackPressed(); 
     //} 
    } 

    @Override 
    protected void onResumeFragments() { 
     super.onResumeFragments(); 
    } 

    /** Configura o List com as informacoes **/ 
    private void configItensListView(){ 
     fragments = new ArrayList<ItensListView>(); 
     //define 
     ItensListView itens0 = new ItensListView("Noticias", R.drawable.setavermelha); 
     ItensListView itens1 = new ItensListView("Eventos", R.drawable.setavermelha); 
     ItensListView itens2 = new ItensListView("Área do Aluno", R.drawable.setavermelha); 
     ItensListView itens3 = new ItensListView("Contato", R.drawable.setavermelha); 
     ItensListView itens4 = new ItensListView("Redes Sociais", R.drawable.setavermelha); 
     ItensListView itens5 = new ItensListView("Sobre", R.drawable.setavermelha); 
     ItensListView itens6 = new ItensListView("Sair", R.drawable.setavermelha); 

     //add 
     fragments.add(itens0); 
     fragments.add(itens1); 
     fragments.add(itens2); 
     fragments.add(itens3); 
     fragments.add(itens4); 
     fragments.add(itens5); 
     fragments.add(itens6); 

     //AreaAlunoFrag 
    } 

    private void closeApp(){ 
     System.exit(0); 
    } 

    @Override 
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) { 
     //super.onSaveInstanceState(outState, outPersistentState); 
     getSupportFragmentManager().beginTransaction().commitAllowingStateLoss(); 
    } 

    private void removeAllFrags(){ 
     FragmentManager fm = getSupportFragmentManager(); 
     for(int x = 0; x < fm.getBackStackEntryCount(); x++){ 
      //getSupportFragmentManager().beginTransaction().remove(fm.findFragmentById(x)).commit(); 
      fm.popBackStackImmediate(); 
     } 
    } 

    @Override 
    protected void onStart() { 
     new SendProjectId(getApplicationContext()); 
     super.onStart(); 
    } 

    @Override 
    protected void onResume() { 
     PushControl.setIsVisible(true); 
     new SendProjectId(getApplicationContext()); 
     super.onResume(); 
    } 

    @Override 
    protected void onPause() { 
     PushControl.setIsVisible(false);   
     super.onPause(); 
    } 

    @Override 
    protected void onStop() { 
     PushControl.setIsVisible(false); 
     //removeAllFrags(); 
     super.onStop(); 
    } 

    @Override 
    protected void onDestroy() { 
     PushControl.setIsVisible(false); 
     super.onDestroy(); 
     CustomVolleySingleton.getInstance().cancelPendingRequests(CustomVolleySingleton.TAG); 
    } 

} 

Обновление CustomDrawerLayout и манифест - он отлично работает.

CustomDrawerLayout

public class CustomDrawerLayout extends ActionBarActivity implements OnItemClickListener{ 
    private ActionBar ab; 
    private DrawerLayout dl; 
    private ListView lv; 
    private ActionBarDrawerToggle tg; 
    private LinearLayout navdrawer; 

    private List<ItensListView> fragments; 
    private CharSequence tl; //titulo principal 
    private CharSequence tlf; //titulo fragment 

    public static final String APP_NAME = "App"; 
    private static String URL_NOTIFICATION = ""; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_custom_drawerlayout); 
     getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.action_bar))); 
     getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
     getSupportActionBar().setCustomView(R.layout.actionbar_custom); 

     init(); 

     if(savedInstanceState == null){ 
      selectedItem(0); 
     } 
    } 

    /** open fragment by notification */ 
    private void openFragByNotification(){ 

     if(!URL_NOTIFICATION.isEmpty()) { 
      //Open By Notification 
      if (URL_NOTIFICATION.equals("Noticias")) { 
       selectedItem(0); 
      } else if (URL_NOTIFICATION.equals("Eventos")) { 
       selectedItem(1); 
      } else if (URL_NOTIFICATION.equals("Tarefas") || 
        URL_NOTIFICATION.equals("Advertencias") || 
        URL_NOTIFICATION.equals("Agendas")) { 
       Log.i("Notificacao", URL_NOTIFICATION); 
       Log.i("LOGADO NA SESSION", SessionUsuario.isLogged(this) + ""); 

       if(SessionUsuario.isLogged(this)){ 
        Log.i("Notificao Logado", "esta logado na area do aluno"); 
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 
        Fragment frag = AreaAlunoFrag.newInstance(); 
        frag.getArguments().putString("url", URL_NOTIFICATION); 
        ft.replace(R.id.fl, frag); 
        ft.addToBackStack(APP_NAME); 
        ft.commit(); 
       } 
      } 
     } 
    } 


    private void init(){ 
     //actionbar 
     onConfigActionBar(); 
     //listview 
     configItensListView(); 
     //drawerlayout 
     dl = (DrawerLayout)findViewById(R.id.dl); 
     navdrawer = (LinearLayout)findViewById(R.id.navdrawer); 
     //listview 
     lv = (ListView)findViewById(R.id.lv);    
     lv.setAdapter(new DrawerLayoutListViewAdapter(this, fragments)); 
     lv.setOnItemClickListener(this); 
     //drawerlayout 
     //dl = (DrawerLayout)findViewById(R.id.dl); 
     //mDrawerRelativeLayout = (RelativeLayout)findViewById(R.id.left_drawer); 
     //actionbardrawertoggle 
     tg = new ActionBarDrawerToggle(this, dl, R.drawable.ic_launcher, R.string.drawer_open){ 
      public void onDrawerClosed(View view) { 
       ab.setTitle(tl); 
       supportInvalidateOptionsMenu(); 
      } 

      public void onDrawerOpened(View view) { 
       ab.setTitle(tlf); 
       supportInvalidateOptionsMenu(); 
      } 
     }; 
     dl.setDrawerListener(tg); 
     tl = tlf = getTitle(); 
    } 

    /** ativa actionbar e botao home na action bar */ 
    private void onConfigActionBar(){ 
     ab = getSupportActionBar(); 
     ab.setDisplayHomeAsUpEnabled(true); 
     ab.setHomeButtonEnabled(true); 
    } 

    @Override 
    public void onConfigurationChanged(Configuration newConfig) { 
     super.onConfigurationChanged(newConfig); 
     tg.onConfigurationChanged(newConfig); 
    } 

    /** necessario */ 
    @Override 
    protected void onPostCreate(Bundle savedInstanceState) { 
     super.onPostCreate(savedInstanceState); 
     tg.syncState(); 
    } 


    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     if (tg.onOptionsItemSelected(item)) { 
       return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 



    /** necessario */ 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.custom_drawer_layout, menu); 
     return true; 
    } 


    /** necessario */ 
    @Override 
    public boolean onPrepareOptionsMenu(Menu menu) { 
     boolean status = dl.isDrawerOpen(navdrawer); 
     //menu.findItem(R.id.action_settings).setVisible(!status); 
     return super.onPrepareOptionsMenu(menu); 
    } 

    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position,long id) { 
     Log.i("POSITION->", position + ""); 
     selectedItem(position);  
    } 

    /** seleciona o fragment q sera usado */ 
    private void selectedItem(int position){ 
     FragmentTransaction ft; 
     Fragment frag; 
     switch (position){ 
      case 0: 
       ft = getSupportFragmentManager().beginTransaction(); 
       frag = NoticiaFrag.newInstance(); 
       ft.replace(R.id.fl, frag); 
       ft.addToBackStack(APP_NAME); 
       ft.commit(); 
       break; 
      case 1: 
       ft = getSupportFragmentManager().beginTransaction(); 
       frag = EventoFrag.newInstance(); 
       ft.replace(R.id.fl, frag); 
       ft.addToBackStack(APP_NAME); 
       ft.commit(); 
       break; 
      case 2: 
       ft = getSupportFragmentManager().beginTransaction(); 
       frag = LoginFrag.newInstance(); 
       ft.replace(R.id.fl, frag); 
       ft.addToBackStack(APP_NAME); 
       ft.commit(); 
       break; 
      case 3: 
       ft = getSupportFragmentManager().beginTransaction(); 
       frag = ContatoFrag.newInstance(); 
       ft.replace(R.id.fl, frag); 
       ft.addToBackStack(APP_NAME); 
       ft.commit(); 
       break; 
      case 4: 
       ft = getSupportFragmentManager().beginTransaction(); 
       frag = CompartilhaFrag.newInstance(); 
       ft.replace(R.id.fl, frag); 
       ft.addToBackStack(APP_NAME); 
       ft.commit(); 
       break; 
      case 5: 
       ft = getSupportFragmentManager().beginTransaction(); 
       frag = SobreFrag.newInstance(); 
       ft.replace(R.id.fl, frag); 
       ft.addToBackStack(APP_NAME); 
       ft.commit(); 
       break; 
      default: 
       closeApp(); 
       break; 

     } 

     lv.setItemChecked(position, true); 
     setCustomTitle(fragments.get(position).getTexto()); 
     dl.closeDrawer(navdrawer); 
    } 

    /** define o titulo da actionbar */ 
    private void setCustomTitle(String title){ 
     //SpannableString s = new SpannableString(title); 
     // s.setSpan(new TypefaceSpan(this, BatalhaConfigs.FONT), 0, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
     //ab.setTitle(s); 
     //tl = s; 
    } 

    @Override 
    public void onBackPressed() { 
     //if(getSupportFragmentManager().getBackStackEntryCount() > 0){ 
     // getSupportFragmentManager().popBackStackImmediate(); 
     // }else{ 
      super.onBackPressed(); 

     //} 
    } 


    @Override 
    protected void onResumeFragments() { 
     super.onResumeFragments(); 
    } 

    /** Configura o List com as informacoes **/ 
    private void configItensListView(){ 
     fragments = new ArrayList<ItensListView>(); 
     //define 
     ItensListView itens0 = new ItensListView("Noticias", R.drawable.setavermelha); 
     ItensListView itens1 = new ItensListView("Eventos", R.drawable.setavermelha); 
     ItensListView itens2 = new ItensListView("Área do Aluno", R.drawable.setavermelha); 
     ItensListView itens3 = new ItensListView("Contato", R.drawable.setavermelha); 
     ItensListView itens4 = new ItensListView("Redes Sociais", R.drawable.setavermelha); 
     ItensListView itens5 = new ItensListView("Sobre", R.drawable.setavermelha); 
     ItensListView itens6 = new ItensListView("Sair", R.drawable.setavermelha); 

     //add 
     fragments.add(itens0); 
     fragments.add(itens1); 
     fragments.add(itens2); 
     fragments.add(itens3); 
     fragments.add(itens4); 
     fragments.add(itens5); 
     fragments.add(itens6); 


     //AreaAlunoFrag 

    } 

    private void closeApp(){ 
     System.exit(0); 
    } 


    @Override 
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) { 
     //super.onSaveInstanceState(outState, outPersistentState); 
     getSupportFragmentManager().beginTransaction().commitAllowingStateLoss(); 

    } 

    private void removeAllFrags(){ 
     FragmentManager fm = getSupportFragmentManager(); 
     for(int x = 0; x < fm.getBackStackEntryCount(); x++){ 
      //getSupportFragmentManager().beginTransaction().remove(fm.findFragmentById(x)).commit(); 
      fm.popBackStackImmediate(); 
     } 
    } 


    @Override 
    protected void onNewIntent(Intent intent) { 
     URL_NOTIFICATION = intent.getStringExtra("url"); 
     Log.i("URL_NOTIFICATION", URL_NOTIFICATION); 
     super.onNewIntent(intent); 
    } 

    @Override 
    protected void onStart() { 
     new SendProjectId(getApplicationContext()); 
     super.onStart(); 
    } 

    @Override 
    protected void onResume() { 
     PushControl.setIsVisible(true); 
     new SendProjectId(getApplicationContext()); 
     openFragByNotification(); 
     super.onResume(); 
    } 

    @Override 
    protected void onPause() { 
     PushControl.setIsVisible(false); 
     URL_NOTIFICATION = ""; 
     super.onPause(); 
    } 

    @Override 
    protected void onStop() { 
     PushControl.setIsVisible(false); 
     URL_NOTIFICATION = ""; 
     super.onStop(); 
    } 

    @Override 
    protected void onDestroy() { 
     PushControl.setIsVisible(false); 
     super.onDestroy(); 
     CustomVolleySingleton.getInstance().cancelPendingRequests(CustomVolleySingleton.TAG); 
    } 


} 

Manifest

<activity android:name=".menu.CustomDrawerLayout" android:launchMode="singleTop"/> 

ответ

3

Проблема заключается в том, что Android по умолчанию не обеспечивает новый Intent к Activity, который уже работает. Если вы закроете приложение с помощью кнопки «Назад», то Activity будет уничтожен, а в следующий раз будет вызываться startActivity(), и будет создан новый экземпляр Intent. Если вы оставите Activity через домашнюю кнопку, Activity не будет уничтожен. Когда вы вызываете startActivity(), в этом случае уже запущенный экземпляр Activity выведен на передний план, но новый Intent отправлен , а не.

Чтобы изменить это поведение, вы можете определить свою активность в манифесте с launchMode="singleTop":

<activity 
    ... 
    android:launchMode="singleTop" 
    ... 
> 
</activity> 

В этом случае Android будет поставлять новый Intent через onNewIntent(), который вы можете override:

Этот вызывается для действий, которые устанавливают launchMode на «singleTop» в их пакете, или если клиент использовал флаг FLAG_ACTIVITY_SINGLE_TOP при вызове startActivity (Intent). В любом случае, когда активность повторно запускается в верхней части стека действий вместо нового экземпляра запускаемой операции, onNewIntent() будет вызываться в существующем экземпляре с намерением, который использовался для повторного запуска Это.

Только что заметил флаги, которые вы проходите в своем Intent. В этом случае вам, вероятно, просто нужно переопределить onNewIntent() в вашем Activity.

Я сам столкнулся с этой проблемой и задал вопрос here. В этом вопросе есть еще несколько предпосылок, принятый ответ и комментарии.

+0

Большое спасибо. Я обновил решение, что вы предложили, и отлично работает. Я обновил свой пост и с решением. Еще раз спасибо. – FernandoPaiva

+0

Привет. У меня новая проблема. Когда я закрываю приложение с помощью кнопки, перестает работать, и мне нужно щелкнуть значок, чтобы снова открывать приложение для работы, поскольку уведомление не работает, я думаю, что уведомление не создает новую задачу, потому что мне нужен значок клика, чтобы открыть мой приложение снова. Вы можете мне помочь ? – FernandoPaiva

+1

Вы изменили много вещей в коде, поэтому я не уверен на 100%, но похоже, что вы не устанавливаете 'URL_NOTIFICATION' в' onCreate() ', поэтому, когда вы вызываете' openFragByNotification() 'from' onResume()' он пуст. Возможно, недоразумение заключается в том, что 'onNewIntent()' _only_ вызывается, если экземпляр 'Activity' уже существует, а не когда создается новый экземпляр. –

Смежные вопросы