简体   繁体   中英

Android intent bundle always null?

I am running the below code. There are two snippets. The first is how I am setting the bundle and the second how I am retrieving it. For some reason every time i check the bundle it returns as null.

Any advice on why this is or what I have done wrong would be greatly appreciated.

    Intent intent = new Intent(this, com.hom.app.Hom.class);
        Bundle b = new Bundle();
        b.putString("WELL", "yes");
        intent.putExtras(b);
    startActivity(intent);

Retrieving Bundle:

    String well ="";
    Bundle bun = getIntent().getExtras();
    String standard = "yes";
    if(bun != null){       
        Log.v("Bundle", "Contains data");
        well = bun.getString("WELL");
        if(well == null) well = "";
        if(well == standard) method();
    }   

When you are creating your intent, just put the extras right in there. you are trying to access the wrong bundle in your code above. Something like this should work.

    Intent intent = new Intent(this, com.hom.app.Hom.class);
    intent.putExtras("WELL", "yes");
    startActivity(intent);

Don't know why it would be returning null, but your code would not work even if the bundle was coming through correctly because you are doing string comparison with ==

This line:

        if(well == standard) method();

Should be

        if(well.equals(standard)) method();

From the documentation :

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

so instead why don't you just use

intent.putExtra("WELL", "yes");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM