mike.tihonchik
mike.tihonchik

Reputation: 6933

Android: Notification to call Fragment

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.

Upvotes: 0

Views: 898

Answers (1)

Kevin Coppock
Kevin Coppock

Reputation: 134664

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

Upvotes: 2

Related Questions