Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
696 views
in Technique[技术] by (71.8m points)

java - 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();
    }   
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...