Android的:通知调用片段 [英] Android: Notification to call Fragment

查看:98
本文介绍了Android的:通知调用片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以通知通过设置调用活动 的PendingIntent 并使用 setLastestEventInfo()。如果我想打电话给一个特定的片段是什么,这可能吗?是,我可以覆盖任何方法?据我所知片段并不意图直接合作,而是需要由活动主办。

I can get the Notification to call an activity by setting PendingIntent and using setLastestEventInfo(). What if I want to call to a particular fragment, is that possible? Are the any methods that I can override? As far as I know fragments don't work with intent directly, but need to be hosted by activity.

推荐答案

您可以沿着传递片段的完整类名( MyFragment.class.getName())作为额外的意图。然后,有接收意向托管活动,并显示给定为多余的,像这样的片段:

You could pass the full class name of the Fragment (MyFragment.class.getName()) along as an extra in the Intent. Then, have a hosting Activity which receives the intent, and displays the Fragment given as the extra, something like this:

public static final String FRAGMENT_CLASS = "fragment_class";

public void onCreate(Bundle savedState) {
    Intent i = getIntent();
    String fragmentClass = i.getStringExtra(FRAGMENT_CLASS);
    if (!TextUtils.isEmpty(fragmentClass)) {
        Fragment toDisplay = Fragment.instantiate(this, fragmentClass);
        getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.my_root_container, toDisplay, null)
                .commit();
    }
}

这篇关于Android的:通知调用片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆