Max Usanin
Max Usanin

Reputation: 2499

getSerializableExtra return null

My example:

public static final String EXTRA_TARGET_FRAGMENT = "fragment_to_show";

    public static void show(Activity pActivity,
            Class<? extends Fragment> fragment) {
        Intent intent = new Intent(pActivity, HomeActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra(EXTRA_TARGET_FRAGMENT, fragment);
        pActivity.startActivity(intent);
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        mUpcomingTarget = (Class<Fragment>) intent
                .getSerializableExtra(EXTRA_TARGET_FRAGMENT);

}

mUpcomingTarget --> null, I cannot understand what the problem is.

Upvotes: 3

Views: 2506

Answers (3)

alexey
alexey

Reputation: 126

Make sure that all class members of your "fragment" and class itself implements Serializable interface. Object will not serialize it they not and android will not issue any warning on this. Try to serialize your object to array of bytes (Java Serializable Object to Byte Array) and check if it succeed.

Upvotes: 0

Chris Sim
Chris Sim

Reputation: 4132

Check if the String EXTRA_TARGET_FRAGMENT is equals to fragment_to_show in both activities!! it happened to me I had one different capital letter, when I corrected it it worked :)

Upvotes: 0

Grzegorz Żur
Grzegorz Żur

Reputation: 49231

Documentation of putExtra(String, Serializable) says

The name must include a package prefix, for example the app com.android.contacts would use names like "com.android.contacts.ShowAll".

Change your EXTRA_TARGET_FRAGMENT to start with package prefix.

Upvotes: 5

Related Questions