SensorS
SensorS

Reputation: 383

transfer data between two activity

I want to send a data between two activities and based on data I show one view (I have a frameLayout and based on that data I commit a fragment for fill frameLayout). I write this code but I think it is not transferring data between the two activities! because always case 0! is executing

What is the problem??

splash:

private OnClickListener onClickListener=new OnClickListener(){

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Log.i("circuleProduct", "clicked");
        Intent intent;
        switch(v.getId()){
        case R.id.btnCircleProduct:
            intent=new Intent(Splash.this,MainActivity.class);
            startActivity(intent);
            intent.putExtra("value",1 );
            break;
        case R.id.btnCircleIntroduce:
            intent=new Intent(Splash.this,MainActivity.class);
            startActivity(intent);
            intent.putExtra("value", 2);
            break;
        case R.id.btnCircleContact:
            intent=new Intent(Splash.this,MainActivity.class);
            startActivity(intent);
            intent.putExtra("value", 3);
            break;
        case R.id.btnCircleMore:
            intent=new Intent(Splash.this,MainActivity.class);
            startActivity(intent);
            intent.putExtra("value", 4);
            break;
        }
    }

};

Activity:

 public class MainActivity extends FragmentActivity {
      @Override
public void onCreate(Bundle savedInstanceState) {

    FragmentManager fm=getSupportFragmentManager();
    FragmentTransaction ft=fm.beginTransaction();

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
     int value=0;
    Intent intent=getIntent();
    value=intent.getIntExtra("value", 0);

    switch(value){
    case 0:
        ft.add(R.id.frameLayout, new Introduce());
        ft.commit();
        break;
    case 1:
        ft.add(R.id.frameLayout, new Product());
        ft.commit();
    case 3:
        ft.add(R.id.frameLayout, new Contact());
        ft.commit();
    case 4:
        ft.add(R.id.frameLayout, new More());
        ft.commit();
        break;
    }
 }
}

Upvotes: 0

Views: 194

Answers (5)

Md. Monsur Hossain Tonmoy
Md. Monsur Hossain Tonmoy

Reputation: 11085

DO put extra before startActivity

intent=new Intent(Splash.this,MainActivity.class);
intent.putExtra("value", 3);
startActivity(intent);

Also give break in case statement, in switch case statement you are missing 2

switch(value){
case 1:
    ft.add(R.id.frameLayout, new Introduce());
    ft.commit();
    break;
case 2:
    ft.add(R.id.frameLayout, new Product());
    ft.commit();
    break;
case 3:
    ft.add(R.id.frameLayout, new Contact());
    ft.commit();
    break;
case 4:
    ft.add(R.id.frameLayout, new More());
    ft.commit();
    break;
}

Upvotes: 1

Lazy Ninja
Lazy Ninja

Reputation: 22537

in main activity you have forgotten the break in switch. That means commit will be called several times. Add break to case1 and case3. And also add the extras before calling startactivity

Upvotes: 0

nahtim
nahtim

Reputation: 96

Do this

intent.putExtra("value",1 );    
startActivity(intent);

instead of

startActivity(intent);    
intent.putExtra("value",1 );  

Upvotes: 0

Hamad
Hamad

Reputation: 5152

try to use data transfering between two activities in this way

 Intent i = new Intent(this, ActivityTwo.class);
  i.putExtra("Value1", 1);
  startActivity(i);

and in other activity after oncreate()

Bundle extras = getIntent().getExtras();
int value1 = extras.getint("Value1",0);

see this detailed tutorial on Android Intents

Upvotes: 1

Day
Day

Reputation: 904

Do the intent.putExtra("value", ...); before you start the activity.

Upvotes: 0

Related Questions