开发者

to call another activity

Thanks !!i have two .java classes as trial.java and CommonActivity.java as:

public void onClick(View v) 
{
    switch (v.getId()) 
    {
    case R.id.btnSequence:
        Intent intent1 = new Intent();
        intent1.setClass(this,CommonActivity.class);
        Bundle bun1 = new Bundle();
        bun1.putString("String_key", "value1");
        intent1.putExtras(bun1 );
        startActivity(intent1);
        break;

    case R.id.btnVideo:
        Intent intent2 = new Intent();
        intent2.setClass(this,CommonActivity.class);
        Bundle bun2 = new Bundle();
        bun2.putString("String_key", "value2");
        intent2.putExtras(bun2);
        startActivity(intent2);
        break;

    case R.id.btnInfo:
        Intent intent3 = new Intent();
        intent3.setClass(开发者_开发知识库this,CommonActivity.class);
        Bundle bun3 = new Bundle();
        bun3.putString("String_key", "value3");
        intent3.putExtras(bun3);
        startActivity(intent3);
        break;
    }

}

and in CommonActivity.java i want to do something like this:

public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    Bundle bun = new Bundle();//am i doing wrong here, where are bun1,bun2 and bun3 of my trialactivty.java
    Intent getIntent = new Intent();
    bun = getIntent.getExtras();
    String param = bun.getString("String_key");
    if(param.equals("value1"))
    {
    setContentView(R.layout.main1);
        }
    if (param.equals("value2"))
    {
    setContentView(R.layout.main2); 
    }
            if (param.equals("value3"))
    {
    setContentView(R.layout.main3); 
    }
}

also while searching on net i got confused with getExtras and getextra.can anybody please example the scenario of using getExtra vs getExtras!so that i can decide to use better one in my application


Here's how you retrieve data from the intent in your new activity...

Bundle callerBundle = getIntent().getExtras(); Object obj = callerBundle.getString("id");

This would work if you did something like,

myIntent.putExtra("id", (Object)obj);

in your previous activity

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜