如何从通知中打开当前片段?

3
我制作了一个包含故事的应用程序,其中一些会有文本和音频。我使用片段来进行导航,因此初始时,使用带有taleNames的ListView的片段,当某个被选中后,下一个片段将上传所选故事的文本和音频。对于音频,我使用Service,并且在那里,当audioTales开始播放时,我会推送通知。 我的问题是:当我启动服务并且音频开始播放时,我从我的应用程序隐藏到主屏幕,然后单击工具栏中的通知。我需要该通知将我推回到当前正在播放音频故事的片段,但实际上它将我推回到ListView。 以下是我的通知:
    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);
        }
在MainActivity的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));

        }
如果需要完整的项目,请访问 GitHub:https://github.com/radric/Tales_updated.git
1个回答

1
你应该尝试启动包含片段的活动,并添加额外参数来检测要显示列表视图或音频视图的片段,类似以下内容:
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);
在您的活动中。
@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));

    }
}
希望这可以帮到你,

谢谢回答!我不确定我是否正确理解了您的意思,所以根据您的回答更新了我的问题,但这并没有帮助到我。 - Stan Malcolm
viewmessage.class 的意思是我的 Fragment 类吗? - Stan Malcolm
抱歉我的思维比较简单,但是“// extract the extra-data in the Notification”这句话怎么帮助我打开当前的片段呢? 还是说我必须将代码更改为类似下面的内容:Fragment fragment = new Fragment(); FragmentManager fragmentManager = getSupportFragmentManager(); fragmentManager.beginTransaction() .replace(R.id.container,fragment) .commit(); - Stan Malcolm
让我们在聊天中继续这个讨论 - IshRoid
我试图创建SharedPreferences,但它导致了NPE的新问题,我意识到我们移动的方向有点错误...实际上,我们不需要调用新的Fragment。我们所需要的只是从内存堆栈中调用当前的Fragment。 - Stan Malcolm
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,