3

i make an app with tales and some of the tham will have text and audio. ol navigation i make in fragments, so at first - fragment with listView with taleNames, when some chosen - next fragment in which i will upload text and audio for chosen tale. for audio i am using Service, and also there i push notification when audioTales started.

my problem is next: when i startService and audio begin to play, i am hide from my app to main screen and than click on notification in toolBar. i need that notification push me to my current fragment where audio Tale is playing now, but in fact it push me to ListView.

here is my notification:

    private void initNotification(){
            CharSequence tikerText = getResources().getString(R.string.tickerText);
            NotificationManager mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
            Notification notification = new Notification(R.drawable.ic_launcher,tikerText,System.currentTimeMillis());
            notification.flags = Notification.FLAG_ONGOING_EVENT;
            Context context = getApplicationContext();
            CharSequence contentTitle = getResources().getString(R.string.contentTitle);
            CharSequence contentText = getResources().getString(R.string.contentText);
            Intent notifIntent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
            notifIntent.putExtra("showAudioFrag",true);
            PendingIntent contentIntent = PendingIntent.getActivity(context,0,notifIntent,0);
            notification.setLatestEventInfo(context,contentTitle,contentText,contentIntent);
            mNotificationManager.notify(NOTIFICATION_ID,notification);
        }

next code iam added in MainActivity in OnCreate:

intent = getIntent();
        boolean reloadFragmentFromNotification = intent.getBooleanExtra("showAudioFrag",false);
        if (reloadFragmentFromNotification){
            Fragment fragment = new TaleActivity_Audio();
            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction()
                    .replace(R.id.container,fragment)
                    .commit();
        } else {

            mNavigationDrawerFragment = (NavigationDrawerFragment)
                    getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
            mTitle = getTitle();

            // Set up the drawer.
            mNavigationDrawerFragment.setUp(
                    R.id.navigation_drawer,
                    (DrawerLayout) findViewById(R.id.drawer_layout));

        }

if needed full project on gitHub: https://github.com/radric/Tales_updated.git

Stan Malcolm
  • 2,740
  • 5
  • 32
  • 53

1 Answers1

1

You should try to start your fragment containing activity. and put extra pram to detect which fragment has to be display listview or audioview,

something like

Intent notificationIntent = new Intent(getApplicationContext(), viewmessage.class);
notificationIntent.putExtra("showAudioFrag", true);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity(getApplicationContext(),notificationIndex,notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(getApplicationContext(), notificationTitle, notificationMessage, pendingNotificationIntent);

In your activity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);    
    onNewIntent(getIntent());
}

@Override
public void onNewIntent(Intent intent){
    Bundle extras = intent.getExtras();
    boolean reloadFragmentFromNotification = extras .getExtra("showAudioFrag",false);
    if (reloadFragmentFromNotification){
        Fragment fragment = new TaleActivity_Audio();
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.container,fragment)
                .commit();
    } else {

        mNavigationDrawerFragment = (NavigationDrawerFragment)
                getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
        mTitle = getTitle();

        // Set up the drawer.
        mNavigationDrawerFragment.setUp(
                R.id.navigation_drawer,
                (DrawerLayout) findViewById(R.id.drawer_layout));

    }
}

Hope this will help you,

IshRoid
  • 3,696
  • 2
  • 26
  • 39
  • thaks for answer! i am not shure that that i am understand you correct, so i am update my ask according to your answer, but it doesn't help. – Stan Malcolm Feb 27 '15 at 15:37
  • than you for answer! i am not shure that i am understand you correct, so i update my ask according to your answer, but it doesn't help... i fact `boolean reloadFragmentFromNotification` in MainActivity is always false. i think i do something wrong... – Stan Malcolm Feb 27 '15 at 15:40
  • Try my updated answer, this may will help you, i think. – IshRoid Feb 27 '15 at 17:10
  • does viewmessage.class mean my fragment class? – Stan Malcolm Feb 27 '15 at 17:14
  • No it is your activity class or try using your current code as getLauncherActivity. – IshRoid Feb 27 '15 at 17:16
  • sorry for my primitive thinking, but `// extract the extra-data in the Notification` - how it will help me to open my current fragment? or i must change that code on something like next: `Fragment fragment = new Fragment(); FragmentManager fragmentManager = getSupportFragmentManager(); fragmentManager.beginTransaction() .replace(R.id.container,fragment) .commit();` ?? – Stan Malcolm Feb 27 '15 at 17:34
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/71894/discussion-between-ishrat-and-andriy-antonov). – IshRoid Feb 27 '15 at 18:19
  • i tried to make SharedPreferences, but it makes new problem with NPE, and i realized that its a little been a wrong way we are moving... in fact we no needed to call new Fragment. all we need is call current fragment from memory Stack – Stan Malcolm Feb 28 '15 at 07:34